所以我正在遵循一个非常基本的 AngularJS 教程,发现 here
我已经进行了大约第 10 步,但我的 View 中没有显示任何内容。给出了什么?
这是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}
];
最佳答案
问题是您试图在任何 Angular 上下文(即 MainCtrl
Controller )之外声明 $scope.posts
。
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
}];
}
]);
关于javascript - AngularJS 模型未将数据加载到 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31976211/