所以我正在关注一个非常基本的AngularJS教程,找到here
我已经了解了第10步,但我的视图中没有显示任何内容。是什么给了什么?
以下是index.html的内容:
<html>
<head>
<title>My Angular App!</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-app="flapperNews" ng-controller="MainCtrl">
<div ng-repeat="post in posts">
{{post.title}} - upvotes: {{post.upvotes}}
</div>
</body>
</html>
以下是app.js的评论:
angular.module('flapperNews', [])
.controller('MainCtrl', [
'$scope',
function($scope){
$scope.test = 'Hello world!';
}]);
$scope.posts = [
{title: 'post 1', upvotes: 5},
{title: 'post 2', upvotes: 2},
{title: 'post 3', upvotes: 15},
{title: 'post 4', upvotes: 9},
{title: 'post 5', upvotes: 4}
];
答案 0 :(得分:3)
问题在于您尝试在任何角度上下文之外声明$scope.posts
,即MainCtrl
控制器。
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>My Angular App!</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-app="flapperNews" ng-controller="MainCtrl">
<div ng-repeat="post in posts">
{{post.title}} - upvotes: {{post.upvotes}}
</div>
</body>
</html>
JS
angular.module('flapperNews', [])
.controller('MainCtrl', [
'$scope',
function($scope) {
$scope.test = 'Hello world!';
// We are inside a controller here:
$scope.posts = [{
title: 'post 1',
upvotes: 5
}, {
title: 'post 2',
upvotes: 2
}, {
title: 'post 3',
upvotes: 15
}, {
title: 'post 4',
upvotes: 9
}, {
title: 'post 5',
upvotes: 4
}];
}
]);