angularjs 模式服务广播和问题

angularjs modal service broadcast and on issue

我正在使用 angular-modal-service 库。我的逻辑是:当模态打开时,它运行一个从 SomeService$rootScope.$broadcastSomeService 到模态控制器的函数,这样我就可以将资源从服务发送到我的模态控制器。但是,它不会开火。请帮我弄清楚我错过了什么。谢谢。

**服务:**

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

切换功能后它可以工作,但是... 我如何将数据传递给模态?像 ui-bs-modal,他们有决心。

您正在广播来自模态控制器的事件绑定之前的事件。因此,在广播事件之前,请确保已注册事件侦听器(意味着已加载模态控制器)。所以在 showModal 方法之后调用 SomeService.testFunction();

$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.showModal 之前调用了服务函数。尝试更改顺序。那应该没问题。

里面 $scope.show 试试这个顺序

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