我正在尝试进行$http
调用以将JSON存储到Angular应用程序$rootScope
的.run()
函数中,但调用是在加载控制器后完成的,并且我无法使用该调用中的任何数据。谁能帮我解决这个问题?
还尝试查看会发生什么console.log
,"b"
然后输出"a"
for $rootScope.xml
,undefined
var app = angular.module('lobby', ['ngRoute']);
app.run(['$rootScope','$http', function($rootScope, $http) {
$http.get(Main.constants.BASEURL+'x.xml').success(function(xml) {
$rootScope.xml = $.xml2json(xml.data);
console.log('a');
});
}]);
app.controller('CategoriesController', function($rootScope) {
console.log($rootScope.xml);
console.log('b');
});
您可以尝试$watch
打开$rootScope.xml
并监听新值,该值应在$http
调用解析且xml
范围变量值更改时发生。
代码
app.controller('CategoriesController', function($rootScope) {
$rootScope.$watch('xml', function(newValue, oldValue) {
if($rootScope.xml) {
console.log($rootScope.xml);
}
});
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句