在控制器中访问$rootSope时的 angualrJS 未定义或空引用
AngualrJS Undefined or Null Reference When Accessing $rootSope in Controller
AngularJS最新发布候选版本:
我正在将一个 javascript 对象 - 从模块的运行函数中调用 say stuff 到$rootScope中,我相信它应该阻止。 这是代码:
'use strict';
/* App Module */
var app = angular.module('MyApp', ['ngRoute', 'API'])
.run(function ($rootScope, API) {
$rootScope.stuff = null;
// call the API
API.getStuff()
.success(function(data){
$rootScope.stuff = data;
})
.error(function(data){
$rootScope.stuff = null;
});
});
现在,当我尝试从控制器访问 $rootScope 的 stuff 属性时,我在内容上收到"未定义或空引用"错误。 代码如下所示:
'use strict';
app.controller('indexController',
function ($scope, $rootScope, otherAPI) {
var
stuff = $rootScope.stuff;
// call the other API
otherAPI.getDifferentStuff(stuff.property)
.success(function(data){
$scope.differentStuff = data;
})
.error(function(data){
// do some error handling stuff here
});
});
我知道运行函数中的 api 调用成功,并且它正在为$rootScope中的东西分配一个值。 任何人都可以在这里看到我的代码有什么明显的问题吗?
感谢您的任何帮助!
富
API.getStuff
异步 API 调用(看起来像这样)。在这种情况下,您的控制器很可能在异步调用返回之前被初始化,因此 $rootScope.stuff 仍然等于 null。如果您等到调用成功,那么您将拥有您的数据。
相关文章:
- 为什么“;未定义的“;在JavaScript中结束循环
- 要求未定义JS回调参数
- 如何检查管道中未定义的项目
- TypeError:无法读取属性'推'未定义的JavaScript
- $window.ga在AngularJS事件中未定义
- 未捕获的TypeError无法读取未定义的属性socialsharing
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 无法获取属性'selectedIndex'的未定义引用或null引用
- 如何消除代码中的未定义和其他问题
- 未捕获的ReferenceError:$未定义
- this.router在AngularJS 2中未定义
- 未捕获的类型错误:无法读取属性'删除'的未定义
- 节点fs.stat名称未定义
- 尝试在PHP中回显输入文本时出现未定义的索引错误
- Jquery未定义函数正在停止其他操作
- AngularJS指令出错-无法读取属性'编译'的未定义
- 这.SOMETHING 总是返回未定义的 - extjs
- 无法获取属性'Id'使用Knockout.js的未定义或空引用API
- Wordpress中带有JQuery Accordion的未定义匿名函数
- 在控制器中访问$rootSope时的 angualrJS 未定义或空引用