gpt4 book ai didi

javascript - Angular 无法设置未定义的属性

转载 作者:可可西里 更新时间:2023-11-01 02:28:26 25 4
gpt4 key购买 nike

我不太明白为什么以下内容不起作用:

main.html

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

主要.js

angular.module('myApp')
.controller('MainCtrl', function(someService, $location, $scope) {

$scope.message.test = "blablabla";

}
});

当我运行代码时,我会遇到一个错误:

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)

显然我遗漏了一些非常明显的东西..

最佳答案

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

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

关于javascript - Angular 无法设置未定义的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32520271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com