为什么我无法使用以下代码为$ rootScope赋值。
fetchIp().then(function(response){
$rootScope.nodeIp = (response.length == 0 ? "localhost" : response[0]);
});
var root = 'http://' + $rootScope.nodeIp + ':8080/';
它显示为'undefined'
答案 0 :(得分:1)
api调用是异步的。因此,当您在git submodule sync --recursive
git submodule update --recursive
旁边使用$rootScope.nodeIp
时,.then
未定义。您可以使用承诺或我所采取的方式来实现它。
var root = null;
fetchIp().then(function(response){
$rootScope.nodeIp = (response.length == 0 ? "localhost" : response[0]);
root = 'http://' + $rootScope.nodeIp + ':8080/';
});
答案 1 :(得分:0)
您可以使用模式async / await。
{{1}}
更多信息: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function