angular.js - 请问这里的controlller和directive是怎么影响的?

浏览:25日期:2022-12-29

问题描述

小白一枚,最近在玩angular。。。写个小demo遇到了个问题:为什么这里我加上了controller之后directive的绑定事件就无效的呢。。。求解答。。。

html的代码:

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title></title> <script src='https://www.6hehe.com/wenda/js/angular.min.js' type='text/javascript'></script> <script src='https://www.6hehe.com/wenda/js/directives.js' type='text/javascript'></script> <!--<script>var app=angular.module(’mySystem’,[]);app.controller('test',function ($scope) { $scope.logState=false;}) </script>--></head><body ng-app='mySystem'><form role='search' ng-controller='test' > <p ><input type='text' placeholder='用户名' ng-model='userName'><input type='text' placeholder='密码' ng-model='passWord'> </p> <button type='submit' login-check>登录</button></form><p>{{logState}}</p></body></html>directives.js的代码

var app=angular.module(’mySystem’,[]);app.directive('loginCheck',function () { return{restrict:'A',link:function (scope, element, attrs) { element.bind(’click’,function () {alert(’click’); })} }});

就是简单的在form里面的按钮加个click绑定事件。如果我刚开始没有在加上form那里加上ng-controller=“test”的话,这个directive是可以成功触发的,但是加上之后就无法起作用了。。。请问是什么原因呢?是不是angular的form有默认的方法之类呢(瞎猜的)?求大神帮帮忙。。。。

问题解答

回答1:

好吧。我自己找到答案了。。。只要把directive跟在controller后面写就不会出现这个问题。。。至于为什么这样我还在找答案。欢迎大家来评论或提供答案!---------------------------我是分割线-------------------------------------找到原因了。。。我对模块的理解错了,这个问题可以关闭了。。。

相关文章: