AngularJS指令不能正常工作

问题描述:

请找我写的指令之下,AngularJS指令不能正常工作

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'。

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

声明指令时应该有骆驼案例名称(setFirstActive)。

请参阅developer guide on directives

指令具有骆驼名称,如'ngBind'。可以通过将骆驼案例名称转换为具有以下特殊字符的蛇情况来调用该指令:, - 或_。可选地,该指令可以用x-或数据作为前缀,以使其符合HTML验证器。这里列出了一些可能的指令名称:ng:bind,ng-bind,ng_bind,x-ng-bind和data-ng-bind。

+0

感谢您的回复Artem。只有一个查询,没有设置 - 首先有效的名字可能吗?我们已经有可能的指令名称,如ng-bind,x-ng-bind,它们与set-First-Active类似。 – 2012-08-18 11:09:50

+1

@Abdul当你声明指令时,它被命名为'setFirstActive'。当你调用指令时,你可以使用'set-first-active','set_first_active','数据优先','x-first-active'。 – 2012-08-18 12:20:01

+0

现在明白了。感谢您再次澄清它。 – 2012-08-18 18:17:52