简体   繁体   English

为什么不能将ng-click附加到我的元素?

[英]Why can I not attach ng-click to my element?

The ng-click event is not firing on my element. ng-click事件未在我的元素上触发。 I have tried the in-line controller script given below. 我尝试了下面给出的嵌入式控制器脚本。 I am unable to hit my alert. 我无法发出警报。 Is there any integration problems I should be wary of? 我应该警惕任何集成问题吗? What could be the issue here? 这里可能是什么问题?

 <a class="button black"><div class="light" ng-click="AddCrims()"></div>Add Crims</a>

myApp.controller('MyCtrl', ['$scope', '$http', function ($scope, $http) {
    $scope.AddCrims = function () {
        alert('hi');
     }
});

Appreciated if anyone helps me this. 感谢有人帮助我。 thanks. 谢谢。

Assuming all essentials are correct eg ng-app="myApp" and in some way your controller is registered properly eg ng-controller="MyCtrl" , you need to add ng-click on your <a> - not <div> (which appears empty). 假设所有要素都是正确的,例如ng-app="myApp"并以某种方式正确注册了控制器,例如ng-controller="MyCtrl" ,则需要在<a>上添加ng-click ng-controller="MyCtrl"而不是<div>显示为空)。 Observe the following... 注意以下几点...

<a class="button black" ng-click="AddCrims()"><div class="light"></div>Add Crims</a>

JSFiddle Link - working demo JSFiddle Link-工作演示


  • Note: if you intend to attach the event to the <div> - you'll need to ensure it's not an empty element some way or another eg define static height/width. 注意:如果您打算将事件附加到<div> -您需要确保它不是某种空元素,例如,定义静态高度/宽度。 As a further observation, it'd be worth noting that anchor tags should have a href="" attribute, even if empty, for accessibility purposes. 作为进一步的观察,值得注意的是,出于可访问性的考虑,锚标记应具有href=""属性(即使为空)。

JSFiddle Link - example <div ng-click="AddCrims()"></div> JSFiddle链接 -示例<div ng-click="AddCrims()"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM