AngularJS 指令不起作用

标签 angularjs angularjs-directive

请在我写的指令下面找到,

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/

相关文章:

javascript - Angular-ui-bootstrap datepicker 指令 - 放在前面

javascript - 单击输入字段一段时间后,模型更改时 AngularJS View 未更新

angularjs - 嵌套和重复的angularjs指令中链接函数的调用顺序

javascript - 错误 : undefined is not an object (evaluating 'ProfileService.getProfile(1).then' )

javascript - AngularJS:避免使用 'angular' 全局对象

javascript - 即使绑定(bind)到更深的对象或数组,也使 ngModel 触发 $render

javascript - Angularjs 文件上传指令

AngularJS 指令中编译器属性的问题

javascript - AngularJS 编辑数据/表单

angularjs - 在 angular ui-router URL 中添加可选属性