M B
M B

Reputation: 2326

pass razor variable to angular function

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)

Upvotes: 0

Views: 2836

Answers (1)

Shyju
Shyju

Reputation: 218762

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.

Upvotes: 3

Related Questions