2016-01-17 80 views
0

下面是我的代碼段,其中我試圖使用uimodal來顯示用戶詳細信息以及其他詳細信息。無法將API響應傳遞給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

你可以添加'usereditService.resp'代碼嗎? –

回答

0

數據來自異步響應,所以不能根據您的請求獲取該Ajax響應數據。

在這種情況下,您需要遵循諾言模式,並從您的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; 
    } 
});