Angular不能设置undefined的属性

时间:2022-08-24 13:21:10

I can't quite figure out why the following isn't working:

我无法弄清楚为什么以下不起作用:

main.html

main.html中

<div class="MainCtrl">
  <h1>{{message.test}}</h1>
</div>

main.js

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:28 at $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)

TypeError:无法在调用时设置undefined属性'test'(http:// localhost:9000 / scripts / controllers / main.js:13:25)(http:// localhost:9000 / bower_components / angular / angular)。 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 at $ 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)在compositeLinkFn(http:// localhost:9000 / bower_components / angular / angular.js:8246:11)的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 )

Clearly I'm missing something super obvious..

显然,我错过了一些非常明显的东西......

4 个解决方案

#1


30  

You're defining a property to an undefined object, you have to initialized your object before setting properties.

您正在为未定义的对象定义属性,您必须在设置属性之前初始化对象。

$scope.message = {};
$scope.message.test = 'bla';

#2


4  

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";

#3


3  

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”;

#4


3  

message in your code is not an object, you must make it:

代码中的消息不是对象,您必须创建它:

$scope.message = { test: 'blablabla' };

#1


30  

You're defining a property to an undefined object, you have to initialized your object before setting properties.

您正在为未定义的对象定义属性,您必须在设置属性之前初始化对象。

$scope.message = {};
$scope.message.test = 'bla';

#2


4  

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";

#3


3  

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”;

#4


3  

message in your code is not an object, you must make it:

代码中的消息不是对象,您必须创建它:

$scope.message = { test: 'blablabla' };