[英]Promise in meanjs app controller “then” is not a function
在我的使用meanjs生成器和自動生成的CRUD模塊創建的meanjs應用程序中,為了獲得下面的收入,我想自定義get方法返回的promise,以便我可以調用方法並對檢索到的數據進行轉換。 所以我嘗試在我的控制器中更改它:
// Find existing Income
$scope.findOne = function() {
Incomes.get({
incomeId: $stateParams.incomeId
}).then(function(income){
$scope.income = income;
$scope.incomeDollar= income*$scope.dollarRate;
});
這只會給我錯誤:undefined不是一個函數,然后指向。 我在這里做錯了什么? 如何對從上述get方法檢索的數據進行轉換?
我想使用的模式是在文章的末尾位置 。 我想從短版本切換到長版本,以便在回調中做更多的事情。
// LONG:
myModule.controller('HelloCtrl', function($scope, HelloWorld) {
HelloWorld.getMessages().then(function(messages) {
$scope.messages = messages;
});
});
為了簡化此過程,本文的作者將“ getMessages”返回的承諾放在范圍內:
// SHORTER:
myModule.controller('HelloCtrl', function($scope, HelloWorld) {
$scope.messages = HelloWorld.getMessages();
});
可以使用$ promise訪問promise對象。 從文檔
資源實例和集合具有以下附加屬性:
$ promise:創建此實例或集合的原始服務器交互的承諾。
成功后,將使用相同的資源實例或集合對象來解決承諾,並使用服務器中的數據進行更新...
因此,您的代碼應如下所示:
// Find existing Income
$scope.findOne = function() {
Incomes.get({
incomeId: $stateParams.incomeId
}).$promise.then(function(income){
$scope.income = income;
$scope.incomeDollar= income*$scope.dollarRate;
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.