admin管理员组文章数量:1330612
I am trying to render the thing like this:
<thead ng-init="isDoctor = @(User.IsInRole("Doctor"))">
I expect it to be "isDoctor = true|false" (my server side code renders this template returning PartialView), btw I always get an error like: Syntax Error: Token 'undefined' not a primary expression at column null of the expression [isDoctor =] starting at [isDoctor = at Error ()]. So, what is the reason for that?
I am trying to render the thing like this:
<thead ng-init="isDoctor = @(User.IsInRole("Doctor"))">
I expect it to be "isDoctor = true|false" (my server side code renders this template returning PartialView), btw I always get an error like: Syntax Error: Token 'undefined' not a primary expression at column null of the expression [isDoctor =] starting at [isDoctor = at Error ()]. So, what is the reason for that?
Share Improve this question edited Dec 13, 2013 at 10:17 starbeast asked Dec 13, 2013 at 9:57 starbeaststarbeast 1033 silver badges8 bronze badges2 Answers
Reset to default 7Try this way:
<thead ng-init="isDoctor = @(User.IsInRole("Doctor") ? "true" : "false")">
Because C# boolean renders with capital first letter:
<thead ng-init="isDoctor = True|False">
And True
or False
is undefined
Alternatively, you might consider the following:
<script type='text/javascript'>
angular.module('my-module')
.value('isDoctor', @(User.IsInRole("Doctor") ? "true" : "false"))
// perhaps other values that need passed in from the server?
.constant('aValue', '@Model.AValue') // for example
;
</script>
This doesn't put the value directly onto the scope like ng-init
does; however, it gives you an injectable value that you can use in your controller, services, directives, etc. that is, effectively, a run-time configuration variable:
// some script somewhere
angular.module('my-module').controller('myController', [
'$scope',
'isDoctor',
function($scope, isDoctor) {
$scope.isDoctor = isDoctor;
// other controller code here
}]);
The nice part about this style of passing data from Razor to Angular is that Angular's dependency injection will throw an error if you forget to do this, since instantiating myController
requires isDoctor
to be defined. It moves what could potentially be a runtime error that's hard to find into a config/pile error that should be relatively easy to correct.
It also gives you a single place in your Razor (cshtml) file where your server-side parameters get passed into the Angular runtime, which is less messy, especially if your HTML markup has lots of directives and interpolation.
本文标签: javascriptnginit with razor expressionStack Overflow
版权声明:本文标题:javascript - ng-init with razor expression - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742225744a2436281.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论