繁体   English   中英

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

在绑定来自模态控制器的事件之前,您正在广播事件。 因此,在广播事件之前,请确保已注册事件侦听器(表示已加载模式控制器)。 因此调用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);
    });
};

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

$scope.show里面试试这个命令

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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