私は、変数mxY
に設定しようとしている約束を返す関数を持っています(JavaScriptが初めてだと思います)。私はこのように私の関数を書いている:外部変数への約束を設定する
function maxYvalue2() {
return Rpt_scn_cost_v.find({filter: { where: {scenario_id: $stateParams.id}}}).$promise.then(function(response){
var maxYvalue = 0
for (var i=0;i<response.length;i++) {
var currMaxYvalue = parseFloat(response[i].cur_cost) + parseFloat(response[i].tgt_cost);
if (currMaxYvalue > maxYvalue) {
maxYvalue = currMaxYvalue
};
}
console.log("yVal: " + maxYvalue)
return maxYvalue;
});
};
var mxY = maxYvalue2().then(function (response) {
console.log("fnc: ", response);
return response;
});
console.log(mxY);
と私のコンソールがmxY
が{$$state: Object}
をログに記録するようだところ、この、示しています。私はこれまで理解して何から
reports.controller.js:99 d {$$state: Object}
$$state: Object
status: 1
value: 78820.3574413
__proto__: Object
__proto__: Object
reports.controller.js:88 yVal: 78820.3574413
reports.controller.js:94 fnc: 78820.3574413
を、maxYvalue2()
で.then
が戻っています私が戻って$$state.Object
を返す理由ですが、私がする必要があるのは、それが解決するときに約束を「解き放つ」ことです、そうか、私はここから完全に離れていますか?そしてそれを変数にセットしましたか?
------ EDIT ---------
私は以下の強調表示された領域にyDomain: [0, mxY]
にyDomain: [0, 100000]
を変更...ここに行くためにmaxYvalue2()
の結果を取得しようとしています:
function maxYvalue2() {
return Rpt_scn_cost_v.find({filter: { where: {scenario_id: $stateParams.id}}}).$promise.then(function(response){
var maxYvalue = 0
for (var i=0;i<response.length;i++) {
var currMaxYvalue = parseFloat(response[i].cur_cost) + parseFloat(response[i].tgt_cost);
if (currMaxYvalue > maxYvalue) {
maxYvalue = currMaxYvalue
};
}
console.log("yVal: " + maxYvalue)
return maxYvalue;
});
};
var mxY = maxYvalue2().then(function (response) {
console.log("fnc: ", response);
return response;
});
console.log(mxY);
$scope.options_scn_cst = {
chart: {
type: 'lineChart',
height: 450,
margin : {
top: 20,
right: 20,
bottom: 40,
left: 55
},
x: function(d){ return d.x; },
y: function(d){ return d.y; },
useInteractiveGuideline: true,
dispatch: {
stateChange: function(e){ console.log("stateChange"); },
changeState: function(e){ console.log("changeState"); },
tooltipShow: function(e){ console.log("tooltipShow"); },
tooltipHide: function(e){ console.log("tooltipHide"); }
},
xAxis: {
axisLabel: '',
tickFormat: function(d) { return d3.time.format('%b %y')(new Date(d)); }
},
yDomain: [0, 100000], //<======change the 100000 to the var mxY
yAxis: {
axisLabel: '$/month',
tickFormat: function(d){
return d3.format('$,.0f')(d);
},
axisLabelDistance: -10
},
callback: function(chart){}
},
title: {
enable: true,
text: 'Scenario Costs Over Time'
},
subtitle: {
enable: false,
text: 'Put your Subtitle here.',
css: {
'text-align': 'center',
'margin': '10px 13px 0px 7px'
}
},
caption: {
enable: false,
html: 'Put your Caption Here.',
css: {
'text-align': 'justify',
'margin': '10px 13px 0px 7px'
}
}
};
「アンラップ」とはどういう意味ですか?あなたの約束事とconsole.logは期待値を示しているようです。 – tsuz
'then'コールバックで値を必要とするすべてのコードを移動します。それは、それを「アンラップする」唯一の方法です。 – Bergi
ok ... 'then()'の中に '$ scope.options_scn_cst'を移動しましたが、今は' $ scope.options_scn_cst'にアクセスできないようです... – user2061886