[英]ng-click does not fire javascript global function
I'm new to AngularJS, and I've put an ng-click on a radio button generated by ng-repeat, and the click event refuses to fire. 我是AngularJS的新手,我点击ng-repeat生成的单选按钮,点击事件拒绝触发。 If I use a simple onclick, that does work though.
如果我使用简单的onclick,那确实有效。
This works, and I see the alert: 这有效,我看到警报:
<div class="span2 left-justify"
ng-repeat="choice in physicalmode_choices">
<input type="radio"
name="physical_layer"
value="{{ choice.private }}"
onclick="alert('foo')"
required
ng-model="$parent.networkoptions.physicalmode" />
<span ng-bind="choice.public"></span>
</div>
But this does not: 但这不是:
<div class="span2 left-justify"
ng-repeat="choice in physicalmode_choices">
<input type="radio"
name="physical_layer"
value="{{ choice.private }}"
ng-click="alert('foo')"
required
ng-model="$parent.networkoptions.physicalmode" />
<span ng-bind="choice.public"></span>
</div>
Can I not do this with an ng-click? 我不能用ng-click做到这一点吗? Or am I misunderstanding what an "expression" is?
或者我误解了“表达”是什么?
Thanks, Mike 谢谢,迈克
To clarify DerekR's answer. 澄清DerekR的答案。
When angular sees 当角度看到
ng-click='alert("test")'
it looks for 它寻找
$scope.alert
which is likely to be undefined. 这可能是未定义的。
You need to provide a proxy method on the scope, or possibly even rootscope. 您需要在范围上提供代理方法,甚至可能在rootscope上提供代理方法。
For example: 例如:
$rootScope.log = function(variable) {
console.log(variable);
};
$rootScope.alert = function(text) {
alert(text);
};
See: http://deansofer.com/posts/view/14/AngularJs-Tips-and-Tricks-UPDATED#scopemethods 请参阅: http : //deansofer.com/posts/view/14/AngularJs-Tips-and-Tricks-UPDATED#scopemethods
When you call something inside of an ng-click the parsing service evaluates expressions against the scope rather than the global window object. 当您在ng-click内部调用某些内容时,解析服务会根据范围而不是全局窗口对象来计算表达式。
If you wanted to do an alert inside of an ng-click then could write a method on the scope or parent scope that in turn calls the alert. 如果您想在ng-click内部发出警报,则可以在范围或父范围上编写一个方法,然后调用该警报。
I created this jsFiddle to show how it works. 我创建了这个jsFiddle来展示它是如何工作的。
http://jsfiddle.net/tM56a/1/ http://jsfiddle.net/tM56a/1/
<li ng-repeat="menu in menus" >
<a ng-click="test(menu)">click me</a>
</li>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.