【发布时间】:2017-08-09 19:58:26
【问题描述】:
我正在使用angular-modal-service 库。我的逻辑是:当模态打开时,它运行来自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;
});
});
};
切换功能后,它可以工作,但是... 我如何将数据传递给模态?就像 ui-bs-modal 一样,他们有决心。
【问题讨论】:
-
您的服务中是否缺少
return this;? -
@PrashantGhimire 不需要从服务中返回
this。service将隐式返回函数的this(context) -
很高兴知道! @PankajParkar ;)
标签: angularjs modal-dialog broadcast angular-broadcast