AngularJS指令不起作用 [英] AngularJS directive is not working

查看:202
本文介绍了AngularJS指令不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请在我写的指令下方找到

Please find below the directive which i wrote,

angular.module('netVogue.directives', []).
  directive('set-First-Active', function() {
      return function(scope, element, attrs){
          alert('sample');
          element.addClass("active");     
      };
  });

我已通过以下方式将此指令添加到我的模块中,

I have added this directive to my module in below way,

angular.module('netVogue', ['netVogue.filters', 'netVogue.services', 'netVogue.directives']);

我在模板中以以下格式使用了该指令,

I used this directive in my template in following format,

<div class="item" ng-repeat="viewPrintcampaign in viewPrintcampaigns" ng-init="first=$first" set-First-Active> 
</div>

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

However, i neither see any response of alert nor class is getting added. Can someone please help me with this? Due to some dependency, i don't want to use 'ng-class' but want to add class='active' for the first element of ng-repeat.

任何帮助将不胜感激.预先感谢.

Any help would be greatly appreciated. Thanks in advance.

推荐答案

在声明指令时应使用驼峰式案例名称(setFirstActive).

When declaring directive should has camel case name (setFirstActive).

请参见开发人员指令指南.

伪指令中包含驼峰式的名称,例如"ngBind".可以通过使用以下特殊字符:,-或_将驼峰大小写名称转换为蛇形大小写来调用该指令.可选地,该指令可以以x或data为前缀,以使其与HTML验证程序兼容.以下是一些可能的指令名称的列表:ng:bind,ng-bind,ng_bind,x-ng-bind和data-ng-bind.

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指令不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆