[英]How to pass guid to angular ng-init
I am trying to pass the following value to angular ng-init method 我正在尝试将以下值传递给angular ng-init方法
ViewBag.payer = "921AC694-BA62-4388-AD92-282BD95526E1"
<input ng-model ="pid" ng-init="updatesubscriber(pid = @ViewBag.payer )"/>
I keep getting the following error: Error: [$parse:syntax] http://errors.angularjs.org/1.2.11/ $parse/syntax?p0=AC694&p1=is%20unexpected%2C%20expecting%20%5B)%5D&p2=21&p3=updatesubscriber(921AC694-BA62-4388-AD92-282BD95526E1)&p4=AC694-BA62-4388-AD92-282BD95526E1) 我不断收到以下错误:错误:[$ parse:syntax] http://errors.angularjs.org/1.2.11/ $ parse / syntax?p0 = AC694&p1 = is%20unexpected%2C%20expecting%20%5B) %5D&P2 = 21&P3 = updatesubscriber(921AC694-BA62-4388-AD92-282BD95526E1)P4 = AC694-BA62-4388-AD92-282BD95526E1)
It seems like angular doesnt like the hyphens in the payer string and probably evaluating the string as an expression.. 似乎angular不喜欢付款人字符串中的连字符,并且可能将字符串作为表达式进行评估。
How can I just pass the ViewBag.payer and prevent this error? 我如何才能通过ViewBag.payer并防止出现此错误?
You nearly have it. 您差一点就拥有了。 When your
<input>
is rendered server side @ViewBag.payer
will be a guid. 呈现
<input>
,服务器端@ViewBag.payer
将成为@ViewBag.payer
。 But you want a string in JavaScript. 但是,您需要JavaScript中的字符串。 So, you just need to wrap quotes around it:
因此,您只需要将引号引起来:
<input ng-model ="pid" ng-init="updatesubscriber(pid = '@ViewBag.payer' )"/>
Then, it should render as: 然后,它应呈现为:
<input ng-model ="pid" ng-init="updatesubscriber(pid = '921AC694-BA62-4388-AD92-282BD95526E1' )"/>
JavaScript doesn't know how to evaluate a block of text that looks like 21AC694-BA62-4388-AD92-282BD95526E1
. JavaScript不知道如何评估看起来像
21AC694-BA62-4388-AD92-282BD95526E1
的文本块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.