无法设置未定义

时间:2016-03-23 05:07:32

标签: javascript angularjs

我无法弄清楚为什么我的HTML无效。我刚刚从github克隆了angular-seed,并尝试进行一些更改以了解有关angular的更多信息。

view2.js

.controller('View2Ctrl', [function($scope) {
  $scope.message='hello every one';
}]);

我刚刚添加了一行代码$ scope.message,我在view2.html

中调用了该消息

view2.html

<p>This is the partial for view 2.</p>
<h1>hello</h1>
<p>{{message}}</p>
<p>
  Showing of 'interpolate' filter:
  {{ 'Current version is v%VERSION%.' | interpolate }}
</p>

我收到错误

  

TypeError:无法设置未定义的属性“message”       在新的(http://localhost:8000/app/view2/view2.js:13:15)       在调用(http://localhost:8000/app/bower_components/angular/angular.js:4560:17)       在Object.instantiate(http://localhost:8000/app/bower_components/angular/angular.js:4568:27)       在http://localhost:8000/app/bower_components/angular/angular.js:9440:28       在链接(http://localhost:8000/app/bower_components/angular-route/angular-route.js:985:26)       at invokeLinkFn(http://localhost:8000/app/bower_components/angular/angular.js:9079:9)       at nodeLinkFn(http://localhost:8000/app/bower_components/angular/angular.js:8566:11)       在compositeLinkFn(http://localhost:8000/app/bower_components/angular/angular.js:7965:13)       在publicLinkFn(http://localhost:8000/app/bower_components/angular/angular.js:7845:30)       at boundTranscludeFn(http://localhost:8000/app/bower_components/angular/angular.js:7983:16)   

我想我错过了什么。

1 个答案:

答案 0 :(得分:3)

如果你是缩小代码,那么在缩小之前进行此更改

.controller('View2Ctrl', ['$scope',function($scope) {   // note $scope added
$scope.message='hello every one';

}]);