[英]How to pass arguments to promise in AngularJS
I've go a small angular app with directive. 我去了一个带有指令的小角度应用程序。 For retriving data from serverside I use ngRoute.
为了从服务器端检索数据,我使用ngRoute。 After retriving data I bind result to a scope property and parse the result with ng-repeat like so:
检索数据后,我将结果绑定到scope属性,并使用ng-repeat解析结果,如下所示:
<div class="col-xs-12" ng-repeat="clsRow in classificatorData">
<span>{{clsRow.code}}</span>
</div>
This function that retrievs data from resource 此功能从资源中检索数据
var getClassificatorDataScope = function (criteria, predicate) {
references.initialize('classificators');
references
.getRefereces(null, $scope.classificatorType, criteria, predicate == null ? "NONE" : predicate, $scope.limitLevel, null)
.$promise.then(function (result) {
$scope.classificatorData = result.Data;
});
};
Everything works fine. 一切正常。 But if I try to implement passing result data container (dataScope) like so
但是如果我尝试像这样实现传递结果数据容器(dataScope)
var getClassificatorDataScope = function (criteria, predicate, dataScope) {
references.initialize('classificators');
references
.getRefereces(null, $scope.classificatorType, criteria, predicate == null ? "NONE" : predicate, $scope.limitLevel, null)
.$promise.then(function (result) {
dataScope = result.Data;
});
};
And use it in controller like so 像这样在控制器中使用它
getClassificatorDataScope("CODE", null, $scope.classificatorData);
I've got no data at all. 我什么都没有。 Please help me to understand such behaviour.
请帮助我了解这种行为。
There's 2 problems here. 这里有两个问题。
dataScope = result.Data;
The first one is this. 第一个是这个。 This doesn't act like how you'd expect it would.
这并不像您期望的那样。 It doesn't replace the
$scope.classificatorData
. 它不会替换
$scope.classificatorData
。 All it does is replace the local dataScope
variable in getClassificatorDataScope
to result.Data
(yes, it's not "passed by reference"). 它所做的只是将
getClassificatorDataScope
的本地dataScope
变量替换为result.Data
(是的,它不是“通过引用传递”)。
Second, you're using promises incorrectly. 其次,您使用的诺言不正确。 You return the promise for listening, not pass the scope to who-knows-where.
您返回的承诺是倾听,而不是将范围传递给谁知道哪里。 Your data layer should not be aware of
$scope
or the UI in general. 您的数据层通常不应该知道
$scope
或UI。 Return the promise to the controller, and have it listen for the data from it. 将promise返回给控制器,并让其监听来自它的数据。
// In your function
var getClassificatorDataScope = function(criteria, predicate) {
references.initialize('classificators');
return references
.getRefereces(null, $scope.classificatorType, criteria, predicate == null ? "NONE" : predicate, $scope.limitLevel, null)
.$promise
};
// In your controller
getClassificatorDataScope("CODE", null).then(function(result){
$scope.classificatorData = result.Data;
});
The problem might be in your references.getRefereces method. 问题可能出在您的reference.getRefereces方法中。 It should return a promise and later resolve it with the proper result(I see you try to access "Data" attribute from result.).
它应该返回一个Promise,然后用适当的结果来解决它(我看到您尝试从result中访问“ Data”属性。)。 something like this:
像这样的东西:
reference.getReferences = function() {
var deferred = $q.defer();
someAsyncOperations(function callback(){
deferred.resolve({Data: result}) // make sure you have the "Data" attr
})
return deferred.promise;
// or if someAyncOperations already return a promise
// return someAsyncOperations.then(function(result) {
// return {Data: result};
// });
}
Seems that in second example you're trying to assign data retrieved from server to dataScope
but since AJAX data loading is asynchronoys so $promise
is resolved later than your template with ng-repeat
is drawn. 似乎在第二个示例中,您尝试将从服务器检索到的数据分配给
dataScope
但是由于AJAX数据加载是异步的,因此$promise
的解析要晚于使用ng-repeat
绘制的模板。
There's not enough code provided in question to write whole example - how it should be implemented. 没有足够的代码来编写整个示例-应该如何实现。 But basically you should return
$promise
from your service and in controller change $scope variables upon 但是基本上,您应该从服务中返回
$promise
,并在控制器中更改$ scope变量
promise.then(function() {
//do stuff with $scope variables
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.