Javascript Angular 无法设置未定义的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32520271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular cannot set property of undefined
提问by Paul Boutes
I can't quite figure out why the following isn't working:
我不太明白为什么以下不起作用:
main.html
主文件
<div class="MainCtrl">
<h1>{{message.test}}</h1>
</div>
main.js
主文件
angular.module('myApp')
.controller('MainCtrl', function(someService, $location, $scope) {
$scope.message.test = "blablabla";
}
});
When I run the code I can an error that states:
当我运行代码时,我可以看到一个错误:
TypeError: Cannot set property 'test' of undefined at new (http://localhost:9000/scripts/controllers/main.js:13:25) at invoke (http://localhost:9000/bower_components/angular/angular.js:4473:17) at Object.instantiate (http://localhost:9000/bower_components/angular/angular.js:4481:27) at http://localhost:9000/bower_components/angular/angular.js:9108:28at $route.link (http://localhost:9000/bower_components/angular-route/angular-route.js:977:26) at invokeLinkFn (http://localhost:9000/bower_components/angular/angular.js:8746:9) at nodeLinkFn (http://localhost:9000/bower_components/angular/angular.js:8246:11) at compositeLinkFn (http://localhost:9000/bower_components/angular/angular.js:7637:13) at publicLinkFn (http://localhost:9000/bower_components/angular/angular.js:7512:30) at name.$get.boundTranscludeFn (http://localhost:9000/bower_components/angular/angular.js:7656:16)
类型错误:无法在调用(http://localhost:9000/bower_components/angular/angular.js)时在新(http://localhost:9000/scripts/controllers/main.js:13:25)处设置未定义的属性“test” 。 js:4473:17) 在 Object.instantiate ( http://localhost:9000/bower_components/angular/angular.js:4481:27) 在http://localhost:9000/bower_components/angular/angular.js:9108: 28在 $route.link ( http://localhost:9000/bower_components/angular-route/angular-route.js:977:26) 在 invokeLinkFn ( http://localhost:9000/bower_components/angular/angular.js: 8746:9) 在 nodeLinkFn ( http://localhost:9000/bower_components/angular/angular.js:8246:11) 在复合链接 Fn( http://localhost:9000/bower_components/angular/angular.js:7637:13) 在 publicLinkFn ( http://localhost:9000/bower_components/angular/angular.js:7512:30) 在名称。 $get.boundTranscludeFn ( http://localhost:9000/bower_components/angular/angular.js:7656:16)
Clearly I'm missing something super obvious..
显然我错过了一些非常明显的东西..
回答by Paul Boutes
You're defining a property to an undefinedobject, you have to initialized your object before setting properties.
您正在为未定义的对象定义一个属性,您必须在设置属性之前初始化您的对象。
$scope.message = {};
$scope.message.test = 'bla';
回答by slomek
You should initialize your $scope.message
object, but do it safely (just in case it's defined somewhere else, maybe in some circumstanes):
您应该初始化您的$scope.message
对象,但要安全地进行(以防万一它在其他地方定义,也许在某些情况下):
$scope.message = $scope.message || {};
$scope.message.test = "blablabla";
回答by John V
message
in your code is not an object, you must make it:
message
在你的代码中不是一个对象,你必须让它:
$scope.message = { test: 'blablabla' };
回答by macrog
your test does not exist inside a message, you need to do $scope.message = {};
before , $scope.message.test = "blablabla";
您的测试不存在于消息中,您需要在此$scope.message = {};
之前进行,$scope.message.test = "blablabla";