Friday, 3 August 2018

angularjs: broadcast from directive to controller

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 ?



from angularjs: broadcast from directive to controller

No comments:

Post a Comment