简体   繁体   English

AngularJS中的多个指令

[英]Multiple Directives In AngularJS

I have defined two directives for 2 divs: one for validating phone number and another for email validation. 我为2个div定义了两个指令:一个用于验证电话号码,另一个用于电子邮件验证。 The problem here is that when I run it, it will execute only one validation that is given as second in the directives.js file. 这里的问题是,当我运行它时,它将仅执行一个指令,该指令在directives.js文件中作为第二个给出。 In the HTML page I have two different divs for phone number and email. 在HTML页面中,电话号码和电子邮件有两个不同的div。 How can I solve this? 我该如何解决?

<div id=maindiv><div class="number"><input id="Cont_No" name="phone" type="text" data-phone placeholder="ENTER NUMBER" ng-model="phone"/></div><div class="mail"  ><input id="Cust_Email" name="email" type="email" data-email placeholder="ENTER EMAIL" ng-model="email" /></div></maindiv>.

Html is given above .Directive description is given below 上面给出了HTML,下面给出了指令描述

angular.module('myApp.directives', []).directive('email', function() {
return {
    restrice: 'A',
    require: 'ngModel',
    link: function(scope, element, attrs, ctrl) {
        angular.element(element).bind('blur', function() {
//some code
});              
    }            
}        

}); });

angular.module('myApp.directives', []).directive('phone', function() {
return {
    restrice: 'A',
    require: 'ngModel',
    link: function(scope, element, attrs, ctrl) {
      //console.log("helo");
        angular.element(element).bind('blur', function() {
//some code
 });              
    }            
}        
});

here only the phone directive is geting executed.if i rearrange the orser only email directive gets executing. 在这里,只有phone指令被执行。

I'm not sure but I believe the problem might be you are basically overriding the module by calling module('moduleName', []) twice. 我不确定,但是我相信问题可能是您基本上是通过两次调用module('moduleName', [])覆盖模块。

Try calling the first directive with module('moduleName', []) and the second with module('moduleName') 尝试使用module('moduleName', [])调用第一个指令,并使用module('moduleName')调用第二个指令

Hy, i think your mapping something wrong. 嗨,我认为您的映射有误。 Try my example with your code . 用您的代码尝试我的示例 It seems you don't init the modules. 看来您没有初始化模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM