以下是我的代码片段,其中我尝试使用uimodal来显示用户详细信息以及其他详细信息。
我未能将响应数据绑定到uimodal,请帮助解决此问题。
$scope.selectedUserData = '';
$scope.edituser = function (user) {
usereditService.resp(size.userid, function (response) {
if (response != false) {
console.log(response[0]);//Specific user details object from API
selectedUserData = response[0];
}
else {
console.log('no user found');
}
});
$scope.modalInstance = $uibModal.open({
animation: false,
backdrop: 'static',
templateUrl: '/_views/_editUser.html',
controller: 'userController',
size: size,
resolve: {
selectedData: function () {
return $scope.selectedUserData;
}
},
controller: function($scope, selectedData) {
$scope.editobj = selectedData;
}
});
modalInstance.result.then(function (response) {
$scope.selected = response;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
答案 0 :(得分:0)
数据来自异步响应,因此无法在您请求时立即获取该ajax响应数据。
在这种情况下,您需要遵循承诺模式,并从resolve
selectedData
函数返回承诺链模式中的数据。
//assuming usereditService.resp is returning promise object
//it it doesn't returning promise object, you need to create it by custom promise using $q
var userDataPromise = usereditService.resp(size.userid).then(function (response) {
if (response != false) {
console.log(response[0]);//Specific user details object from API
selectedUserData = response[0];
return selectedUserData;
}
else {
console.log('no user found');
return 'no user found';
}
}, function(error){
console.log();
return 'no user found';
});
$scope.modalInstance = $uibModal.open({
animation: false,
backdrop: 'static',
templateUrl: '/_views/_editUser.html',
controller: 'userController',
size: size,
resolve: {
selectedData: function () {
//returning response object here as a promise.
return userDataPromise;
}
},
controller: function($scope, selectedData) {
$scope.editobj = selectedData;
}
});