我正在使用角度模态服务库。我的逻辑是:当模态打开时,它从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一样,他们有决心。
您正在从模式控制器绑定事件之前广播事件。因此,在广播事件之前,请确保事件侦听器已注册(意味着已加载模态控制器(。因此,在showModal
方法之后调用SomeService.testFunction();
。
$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();
}