0

I have a nested directive (with scope:true, i.e child scope) and I want to broadcast an event from parent to the child. So where should i put the scope.$broadcast() and scope.$on() in the directive, should it be in directive controller or in the link method. my current implementation is as follows:

angular.module('mainApp')
        .directive('customElement', [function(){            
            return {
                restrict: 'E',
                controller : function ($scope, httpService) {
                    **var obj = {
                            url : $scope.url,
                            contentType : "application/json; charset=utf-8",
                            responseType : "json",
                            method : "GET"
                        };
                    httpService.makeAjax(obj).then(function (response) {
                        $scope.$broadcast('ready-to-render', response);
                    });**
                }
            };
        }])
        .directive('testDir', [function(){
            return {
                restrict: 'E',
                replace : true,
                template : "<div>{{row | json}}</div>",
                controller : function ($scope) {
                    $scope.$on('ready-to-render', function(e, response) {
                        $scope.row = response;
                        console.log(response);
                    });
                }
            };
        }]);
atul
  • 552
  • 4
  • 16

1 Answers1

0

you can keep them in any of the places ....

the first answer given here is usefull $rootScope.$broadcast vs. $scope.$emit

Community
  • 1
  • 1
jayanthCoder
  • 615
  • 1
  • 6
  • 13