angular $ broadcast事件没有被解雇?

时间:2016-10-21 14:25:57

标签: javascript angularjs

我想将数据从父控制器发送到子控制器,但是它从父控制器发出的事件没有触发它的时间我正在摸不着头脑发现问题但是我失败所以决定在stackoverflow上请求帮助,任何想法下面有什么问题代码?

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);
    });

1 个答案:

答案 0 :(得分:2)

为什么在打开模式时不传递id,如:

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

以这种方式在子组件中获取它: angular.module(' angularModelerApp&#39)

 .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);
    })
}