7

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

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>

但是,我既没有看到任何警报响应,也没有添加课程。有人可以帮我吗?由于某些依赖性,我不想使用“ng-class”,但想为 ng-repeat 的第一个元素添加 class='active'。

任何帮助将不胜感激。提前致谢。

4

1 回答 1

33

声明指令时应该有驼峰式名称 ( setFirstActive)。

请参阅关于指令的开发人员指南

指令具有驼峰式名称,例如“ngBind”。可以通过使用这些特殊字符 :、- 或 _ 将驼峰式名称转换为蛇形大小写来调用该指令。可选地,该指令可以以 x- 或 data- 为前缀,以使其符合 HTML 验证器。以下是一些可能的指令名称的列表:ng:bind、ng-bind、ng_bind、x-ng-bind 和 data-ng-bind。

于 2012-08-13T12:25:51.387 回答