1

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

**服务: **

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 一样,他们有决心。

4

2 回答 2

1

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

$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);
    });
};
于 2017-03-18T07:04:17.483 回答
1

在模态控制器被实例化或创建之前,您正在广播事件,因为之前调用了服务函数ModalService.showModal。尝试更改顺序。那应该可以正常工作。

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

$scope.show = function(){
 ModalService.showModal({
       ....
       // Listen for broadcast event
 });
 SomeService.testFunction();
}
于 2017-03-18T07:04:26.650 回答