簡體   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