请在我写的指令下面找到,
angular.module('netVogue.directives', []).
directive('set-First-Active', function() {
return function(scope, element, attrs){
alert('sample');
element.addClass("active");
};
});
我已通过以下方式将此指令添加到我的模块中,
angular.module('netVogue', ['netVogue.filters', 'netVogue.services', 'netVogue.directives']);
我在我的模板中使用了这个指令,格式如下,
<div class="item" ng-repeat="viewPrintcampaign in viewPrintcampaigns" ng-init="first=$first" set-First-Active>
</div>
但是,我既没有看到任何警报响应,也没有添加类(class)。
有人可以帮我吗?
由于某些依赖性,我不想使用“ng-class”,但想为 ng-repeat 的第一个元素添加 class='active'。
任何帮助将不胜感激。提前致谢。
最佳答案
当声明指令应该有驼峰式名称( setFirstActive
)。
见 developer guide on directives .
Directives have camel cased names such as 'ngBind'. The directive can be invoked by translating the camel case name into snake case with these special characters :, -, or _. Optionally the directive can be prefixed with x-, or data- to make it HTML validator compliant. Here is a list of some of the possible directive names: ng:bind, ng-bind, ng_bind, x-ng-bind and data-ng-bind.
关于AngularJS 指令不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11933169/