我正在使用angular-modal-service库。我的逻辑是:当模态打开时,它运行一个函数从SomeService
,$rootScope.$broadcast
从SomeService
运行到模态控制器,这样我就可以将资源从服务发送到我的模态控制器。但是,它并没有发射。请帮我弄清楚我错过了什么。谢谢。
**服务:**
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一样,他们有决心。
答案 0 :(得分: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);
});
};
答案 1 :(得分:1)
在实例化或创建模态控制器之前,您正在广播事件,因为在ModalService.showModal
之前调用了服务函数。尝试更改订单。这应该可以正常工作。
在$scope.show
内尝试此订单
$scope.show = function(){
ModalService.showModal({
....
// Listen for broadcast event
});
SomeService.testFunction();
}