像这样的函数: ng-click="alert('Clicked')" [英] A function like: ng-click="alert('Clicked')"
问题描述
对于模型,我需要一个简单的机制,比如
For a mockup I need a simple mechanism like
ng-click="alert('Clicked')"
但是上面的代码不起作用,有人可以帮助我吗?我不想碰控制器..
but the code above is not working, can someone help me? I don't want to touch the Controller..
推荐答案
据我所知,ng-click
仅适用于您的 $scope
.所以你只能调用在你的 $scope
中定义的函数.要使用警报,您可以尝试访问 window
元素,只需使用 window.alert('Clicked')
.
As far as I know, ng-click
works only within your $scope
. So you would only be able to call functions, defined in your $scope
itself. To use alert, you may try accessing the window
element, just by using window.alert('Clicked')
.
EIDT:感谢易卜拉欣.我忘了.你不应该使用 window.alert
,只要你不会定义:
EIDT: Thanks to Ibrahim. I forgot. You shouldn't be able to use window.alert
, as far as you won't define:
$scope.alert = window.alert;
在这种情况下,在您的 ng-clicked
指令中使用 alert('Clicked')
应该可以工作.但最终,这种方法并不能解决您不触摸控制器的问题.
In this case, using alert('Clicked')
in your ng-clicked
directive should work. But at the end, this method would not solve your problem of not touching your controller.
这篇关于像这样的函数: ng-click="alert('Clicked')"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!