javascript - 未定义不是对象 - Angular $rootScope.user.userMessage

标签 javascript angularjs rootscope

我不断收到错误

Error: undefined is not an object (evaluating '$rootScope.user.userMessage')

使用此代码:

if (typeof($rootScope.user.userMessage) === null) {

但如果我在 $rootScope.user.userMessage 中有一些东西,它就不会崩溃。我如何执行检查以查看它是否未定义或为空,以便事情不会中断?

我尝试过的其他事情包括:

if (typeof($rootScope.user.userMessage) === undefined) {
if ($rootScope.user.userMessage === undefined) {

最佳答案

if($rootScope.user && $rootScope.user.userMessage){
    ...
}

关于javascript - 未定义不是对象 - Angular $rootScope.user.userMessage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33822570/

相关文章:

javascript - 如何在javascript中获取浏览器滚动条的高度

javascript - 在 JavaScript/jQuery 中向 API 发出 POST 请求

php - javascript 问题,通过 <php echo ?> 发布到 facebook API,需要转义 ' and "

javascript - Angular - 从函数返回promise.then

javascript - AngularJS 工厂 $rootScope.$on 清理

javascript - 使用 jquery 定位类

javascript - ng-repeat 和搜索过滤结果导致的无限循环

javascript - 观看后从以前的 addGeoJson(scope.data) 中清除谷歌地图

angularjs - 使用 angularJS 中的 rootscope 将值从一个页面传递到另一个页面

javascript - 观察 angularjs $rootScope 变量