0

razor code
@{string razorVar= "test";}

HTML
<div ng-class="{active:vm.testSelected(@razorVar)}"> ...</div>

angular controller function
vm.testSelected = function(jsVar) { return jsVar== 'test'; }

jsVar is always undefined

Question
What is the correct way to pass this razorVar to my function? (I do not want to use an ng-init)

1 Answer 1

3

The markup generated from your code is

<div ng-class="{active:vm.testSelected(test)}"> ...</div>

So the javascript engine is going to assume that test is a javascript variable. But since you did not define a javascript variable called test earlier in the page, it will be undefined.

Pass the value as a string

<div ng-class="{active:vm.testSelected('@razorVar')}"> ...</div>

Now the markup generated will be

<div ng-class="{active:vm.testSelected('test')}"> ...</div>

And your testSelected method will get a valid string value in jsVar parameter.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.