无法将 API 响应传递给 uimodal

Unable to pass API response to uimodal

下面是我的代码片段,我在其中尝试使用 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());
    });

};

数据来自异步响应,因此无法在您请求时立即获取 ajax 响应数据。

在这种情况下,您需要遵循承诺模式,并且 return 来自您的 resolveselectedData 函数的承诺链模式中的数据。

//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;
    }
});