angularjs模式服务广播和发布

angularjs模式服务广播和发布,angularjs,modal-dialog,broadcast,angular-broadcast,Angularjs,Modal Dialog,Broadcast,Angular Broadcast,我正在使用图书馆。我的逻辑是:当模态打开时,它从SomeService运行一个函数,从SomeService运行$rootScope.$broadcast到模态控制器,这样我就可以从服务向模态控制器发送资源。然而,它不会开火。请帮我找出我错过了什么。多谢各位 **服务:** angular.module('ng-laravel').service('SomeService', function($rootScope, Restangular, CacheFactory, $http) {

我正在使用图书馆。我的逻辑是:当模态打开时,它从
SomeService
运行一个函数,从
SomeService
运行
$rootScope.$broadcast
到模态控制器,这样我就可以从服务向模态控制器发送资源。然而,它不会开火。请帮我找出我错过了什么。多谢各位

**服务:**

angular.module('ng-laravel').service('SomeService', function($rootScope, Restangular, CacheFactory, $http) {
     this.testFunction = function() {
        console.log("from service");
        $rootScope.$broadcast('event', {success:'success'});
     };
}
**控制器:**

$scope.show = function(customer_id) {

        ModalService.showModal({
            templateUrl: 'modal.html',
            inputs: {
                customer_id: customer_id
            },
            scope: $scope,
            controller: function($scope, close) {
                $scope.customer_id = customer_id;
                $scope.close = function(result) {
                    close(result, 500); // close, but give 500ms for bootstrap to animate
                };
                $scope.$on('event', function(event, data){
                    alert('yes');
                    console.log('from modal controller');
                });
            }
        }).then(function(modal) {
        SomeService.testFunction(customer_id, tour_id);
            modal.element.modal();
            modal.close.then(function(result) {
                $scope.message = "You said " + result;
            });
        });
    };
切换功能后,它会工作,但。。。
如何将数据传递到modal?与ui bs modal一样,它们也有解析。

在modal控制器的事件绑定之前,您正在广播事件。所以,在广播事件之前,请确保已注册事件侦听器(表示已加载模态控制器)。所以调用
SomeService.testFunction()
showmodel
方法之后

$scope.show = function(customer_id) {
    ModalService.showModal({
        templateUrl: 'modal.html',
        inputs: {
            customer_id: customer_id
        },
        scope: $scope,
        controller: function($scope, close) {
           //code as is
           //listeners will get register from here.
        }
    })
   .then(function(modal) {
       SomeService.testFunction(); //broadcasting event
    }).catch(function(error) {
      // error contains a detailed error message.
      console.log(error);
    });
};

在实例化或创建模式控制器之前,您正在广播事件,因为在
ModalService.showmodel
之前调用了服务函数。试着改变一下顺序。这应该行得通

$scope.show内
尝试此顺序

$scope.show = function(){
 ModalService.showModal({
       ....
       // Listen for broadcast event
 });
 SomeService.testFunction();
}

您是否缺少
返回此信息
在您的服务中?@PrashantGhimire无需从服务中返回此
<代码>服务
将隐式返回函数的
(上下文)show.modal
promise链接,因为它必须异步获取模板URL。@georgeawg嘿,谢谢你的提醒。感谢你的收获……)我错过了那件蠢事;)那太好了,它起作用了。我花了几个小时(原因是,我把它放在模态开放部分的顶部,我不想把参数传递给控制器。似乎我必须这样做。你说的是哪些参数