简体   繁体   中英

angular $broadcast event is not firing?

I want to send data from parent to child controller but its not firing event from parent controller its been hours i am scratching my head find the problem but i failed so decided to ask help on stackoverflow, Any idea what is wrong in below code ?

ParentCtrl.js

angular.module('angularModelerApp')
    .controller('ModelerCtrl', ['$scope', '$state','$log', 'toastr', 'FileSaver', 'Blob', '$uibModal', '$rootScope', '$timeout', function($scope, $state, $log, toastr, FileSaver, Blob, $uibModal, $rootScope, $timeout) {

            $scope.deleteXml = function(id, toast) {
                var id = $scope.diagramObj._id;
                $scope.modalInstance = $uibModal.open({
                    templateUrl: 'app/modeler/modelerDialog/modelerDialog.html',
                    controller: 'ModelerDialogCtrl'
                });
                $timeout(function() {
                    $rootScope.$broadcast('delete-diagram', {
                        id: id
                    });
                });
            }
        });

childCtrl.js

angular.module('angularModelerApp')
  .controller('ModelerDialogCtrl', function ($scope, $uibModalInstance,$log,diagramService,$rootScope) {

    $scope.cancel = function() {
      $uibModalInstance.dismiss('cancel');
    };
    $scope.$on('delete-diagram',function(e,data){
      console.log('in $on',data);
    });

Why dont you pass the id when you open the modal such as:

$scope.modalInstance = $uibModal.open({
  templateUrl: 'app/modeler/modelerDialog/modelerDialog.html',
  controller: 'ModelerDialogCtrl',
  resolve: {
    item: function() {
      return $scope.diagramObj._id
    }
   }
 });

Fetch it in the child component in this way: angular.module('angularModelerApp')

 .controller('ModelerDialogCtrl', function ($scope, $uibModalInstance,$log,diagramService,$rootScope) {

    $scope.cancel = function() {
      $uibModalInstance.dismiss('cancel');
    };
    $scope.$on('delete-diagram',function(e,data){
      console.log('in $on',data);
    });


    $uibModalInstance.result.then(function (data) {
       console.log(data);
    })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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