angularjs:从指令广播到控制器 [英] angularjs: broadcast from directive to controller

查看:32
本文介绍了angularjs:从指令广播到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从指令中向其父控制器发送消息(没有成功)

这是我的 HTML

<我的元素/>

这是控制器中监听事件的代码

$scope.on('go', function(){ .... }) ;

最后的指令看起来像

angular.module('App').directive('myElem',功能 () {返回 {限制:'E',templateUrl: '/views/my-elem.html',链接:函数($scope,$element,$attrs){$element.on('点击', function() {console.log("我们在") ;$scope.$emit('go', { nr: 10 }) ;}}}});

我尝试了不同的范围配置和 $broadcast 而不是 $emit.我看到事件被触发,但控制器没有收到go"事件.有什么建议吗?

解决方案

没有带有作用域的方法 on.在角度它是 $on

下面应该适合你

<html ng-app="测试"><头><script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.2/angular.js"></script><body ng-controller="test" ><我的元素/><!-- 标签 --><脚本>var app = angular.module('test', []);app.controller('test', function ($scope) {$scope.$on('go', function () { alert('event is clicked') });});app.directive('myElem',功能 () {返回 {限制:'E',替换:真,模板:'<div><input type="button" value=check/></input>',链接:函数($scope,$element,$attrs){警报(123");$element.bind('click', function () {console.log("我们到了");$scope.$emit('go');});}}});</html>

Im trying to send a message from within a directive to its parent controller (without success)

Here is my HTML

<div ng-controller="Ctrl">
   <my-elem/>
</div>

Here is the code in the controller which listens for the event

$scope.on('go', function(){ .... }) ;

And finally the directive looks like

angular.module('App').directive('myElem',
   function () {
    return {
        restrict: 'E',
        templateUrl: '/views/my-elem.html',
        link: function ($scope, $element, $attrs) {
            $element.on('click', function() {
                  console.log("We're in") ; 
                  $scope.$emit('go', { nr: 10 }) ;
            }
        }
    }
  }) ;

I've tried different scope configuration and $broadcast instead of $emit. I see that the event gets fired, but the controller does not receive a 'go' event. Any suggestions ?

解决方案

There is no method on with scope. In angular it's $on

below should work for you

<!doctype html>
<html ng-app="test">
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.2/angular.js"></script>

  </head>
 <body ng-controller="test" >    
 <my-elem/>

<!-- tabs -->


 <script>
     var app = angular.module('test', []);
     app.controller('test', function ($scope) {

         $scope.$on('go', function () { alert('event is clicked') });
     });
     app.directive('myElem',
   function () {
       return {
           restrict: 'E',
           replace:true,
           template: '<div><input type="button" value=check/></input>',
           link: function ($scope, $element, $attrs) {
               alert("123");
               $element.bind('click', function () {
                   console.log("We're in");
                   $scope.$emit('go');
               });
               }
       }
   }) ;

   </script>
</body>


</html>

这篇关于angularjs:从指令广播到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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