我正在尝试使用 Angular Bootstrap ui 显示一个模态,它可以解析 Controller 中的项目。问题是我无法在模态 Controller 中获取这些已解决的项目。
$scope.showItemsInBill = function(bill){
var modalInstance = $uibModal.open({
animation: $scope.anismationsEnabled,
resolve: {
items: function(){
var items = [];
BillingService.getItemsInBill(bill).then(function(response){
console.log(response);
return response;
});
}
},
templateUrl: 'templates/showitems-modal.html',
controller: 'ShowItemsModalCtrl',
backdrop: 'static'
});
}
模态 Controller 如下:
angular.controller('ShowItemsModalCtrl', ['$scope', '$uibModalInstance', 'BillingService', 'items', function ($scope, $uibModalInstance, BillingService, items) {
init();
function init(){
console.log('Modal is initializing');
console.log(items);
$scope.items = items;
}
$scope.cancel = function () {
$uibModalInstance.dismiss('cancel');
};
$scope.printBill = function(){
console.log('printing bill');
}
}]);
我在控制台中得到的响应是:
history-controller.js:24 [Object]0: Objectlength: 1__proto__: Array[0]
showitems-modal-controller.js:8 Modal is initializing
showitems-modal-controller.js:9 undefined
所以我从这里理解的是,调用了服务的成功回调,但它没有解析到模态 Controller 中,而是初始化了模态 Controller 。为什么会发生这种情况?
是因为我正在从成功回调返回响应吗?如果是这样,我该怎么办?
最佳答案
您需要在解析中返回 promise
resolve: {
items: function(){
var items = [];
// missing "return"
return BillingService.getItemsInBill(bill).then(function(response){
console.log(response);
return response;
});
}
},
关于javascript - 解析在 Angular ui 模式中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34846087/