[英]Question about node-cache object changing to a resolved promise after assigning its value to a constant
[英]Assigning a value of a promise to an object method
考虑一下我正在创建的这个对象。
$scope.formules = {};
$scope.formules = {
label: 'some label',
getYear: function() {
// getData() is a promise
$scope.getData().then(function(){
// Some code to get the right data
return someData;
}.catch(function () {
$scope.error = 'bad error';
}
}();
}
此时,getYear是未定义的,这很明显,因为我必须进行第二次返回。
$scope.formules = {};
$scope.formules = {
label: 'some label',
getYear: function() {
// getData() is a promise
$scope.getData().then(function(){
// Some code to get the right data
return someData;
}.catch(function () {
$scope.error = 'bad error';
}
return $scope.getData(); //Putting the second return here
}();
或更简单的方法可能是这样的:
$scope.formules = {
label: 'some label',
getYear: function() {
// getData() is a promise
return $scope.getData().then(function(){ // Or putting the second return here.
// Some code to get the right data
return someData;
}.catch(function () {
$scope.error = 'bad error';
}
}();
再次,这不是正确的方法,因为第二次返回仅返回了promise,所以我的方法getYear
现在包含一个promise,而不是正确的方法。
最后尝试:
$scope.formules = {
label: 'some label',
getYear: function() {
// getData() is a promise
return $scope.getData().then(function(){ // return the $scope.getData()
// Some code to get the right data
return someData;
}.catch(function () {
$scope.error = 'bad error';
}
}().then(function(data){ return data; });
现在我没有运气和想法了, console.log($scope.formules)
再次告诉我getYear是一个承诺,不会给我带来价值。
注意:如果我做console.log(data)
而不是return data;
我确实获得了我想绑定到该方法的值。
我在这里做错什么,绑定值的正确方法是什么? Stack&Google没有给我任何答案...
编辑
这是我在方法中使用的实际代码:
getYear: function () {
$scope.measure = value.formules_total_year;
$scope.getCube().then(function(test){
var totalRatioData = [];
for (var i = 0; i < $scope.tempData.length; i++){
totalRatioData.push($scope.tempData[i][1].qNum);
}
return totalRatioData;
})
}(),
$ scope.tempData是一个由5个数组组成的数组。 [Array[2], Array[2], Array[2], Array[2], Array[2]]
,在这5个数组中,您有2个对象
0: Object
qElemNumber: 0
qNum: 2009
qState: "O"
qText: "2009"
__proto__: Object
1: Object
qElemNumber: 0
qNum: 225632.21000000002
qState: "L"
qText: "225632,21"
正如您在此编辑中看到的,我想要的是5个数组的最后一个对象的所有qNum的新数组,并将此数组分配给getYear。
很难理解您最终想要实现什么,以及为什么要“捆绑” getYear()函数中的所有$ scope分配。 在我看来,就像您尝试同步返回异步值(totalRatioData)一样。 这里的要点是,使用es5无法做到这一点。 es7将提供一个称为“ await”的新方法来启用该方法。
如果要在视图上使用getYear的值,只需将promise的值传递给视图模型($ scope变量)。
// init ctrl $scope.year = null; _initYears(); function _initYears() { $scope .getData() .then( function() { var totalRatioData = []; for (var i = 0; i < $scope.tempData.length; i++){ totalRatioData.push($scope.tempData[i][1].qNum); } // assign data to year $scope.year = totalRatioData; } ) ; }
如果您使用的是ui-router,我建议您在状态激活之前,将诺言放在状态配置的所需部分中,以确保已解决。
希望能有所帮助
您不应在范围属性内而是在控制器内调用数据。
function myController($scope, someService) {
$scope.formules = { label : 'label' };
someService.getData().then(function(data) {
$scope.formules.year = getYear(data);
});
}
然后绑定到范围属性
<span ng-bind="formules.label"></span>
<span ng-bind="formules.year"></span>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.