所以,我正在关注位于here的Thinkster的Angular教程。我到了你将帖子折射成服务的部分,突然间我的应用程序被破坏了。这是我的代码(app.js):
var app = angular.module('flapperNews', []);
app.factory('posts', [function(){
var o = {
posts: [];
};
return o;
}]);
app.controller('MainCtrl', [
'$scope', 'posts',
function($scope,posts){
$scope.test = 'Hello world!';
$scope.posts = posts.posts;
$scope.addPost = function(){
if(!$scope.title || $scope.title === '') { return; }
$scope.posts.push({
title: $scope.title,
upvotes: 0,
link: $scope.link
});
$scope.title = '';
$scope.link = '';
}
$scope.incrementUpvotes = function(which){
which.upvotes += 1;
}
}]);
和HTML(index.html):
<html>
<head>
<title> First Angular App </title>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js"></script>
<script src="app.js"></script> <!-- Angular app -->
<style> .glyphicon-thumbs-up { cursor:pointer } </style>
</head>
<body ng-app="flapperNews" ng-controller="MainCtrl">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="page-header">
<h1>Flapper News</h1>
</div>
<div ng-repeat="post in posts | orderBy:'-upvotes'">
<span class="glyphicon glyphicon-thumbs-up"
ng-click="incrementUpvotes(post)"></span>
{{post.upvotes}}
<span style="font-size:20px; margin-left:10px;">
<a ng-show="post.link" href="{{post.link}}">
{{post.title}}
</a>
<span ng-hide="post.link">
{{post.title}}
</span>
</span>
</div>
<form ng-submit="addPost()"
style="margin-top:30px;">
<h3>Add a new post</h3>
<div class="form-group">
<input type="text"
class="form-control"
placeholder="Title"
ng-model="title"></input>
</div>
<div class="form-group">
<input type="text"
class="form-control"
placeholder="Link"
ng-model="link"></input>
</div>
<button type="submit" class="btn btn-primary">Post</button>
</form>
</div>
</div>
</body>
</html>
我尝试了很多不同的东西,移动我的帖子等等(上面列出的代码中没有我的帖子;我把它们拿出来) 我的臭虫在哪里?
如果它有帮助,以下是代码在浏览器中的显示方式:image
答案 0 :(得分:2)
请从posts: [];
var app = angular.module('flapperNews', []);
app.factory('posts', [
function() {
var o = {
posts: []
};
return o;
}
]);
app.controller('MainCtrl', ['$scope', 'posts',
function($scope, posts) {
$scope.test = 'Hello world!';
$scope.posts = posts.posts;
$scope.addPost = function() {
if (!$scope.title || $scope.title === '') {
return;
}
$scope.posts.push({
title: $scope.title,
upvotes: 0,
link: $scope.link
});
$scope.title = '';
$scope.link = '';
}
$scope.incrementUpvotes = function(which) {
which.upvotes += 1;
}
}
]);
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="flapperNews">
<div ng-controller="MainCtrl">
<div ng-repeat="post in posts | orderBy:'-upvotes'">
<span class="glyphicon glyphicon-thumbs-up" ng-click="incrementUpvotes(post)"></span>
{{post.upvotes}}
<span style="font-size:20px; margin-left:10px;">
<a ng-show="post.link" href="{{post.link}}">
{{post.title}}
</a>
<span ng-hide="post.link">
{{post.title}}
</span>
</span>
</div>
<form ng-submit="addPost()" style="margin-top:30px;">
<h3>Add a new post</h3>
<div class="form-group">
<input type="text" class="form-control" placeholder="Title" ng-model="title" />
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Link" ng-model="link" />
</div>
<button type="submit" class="btn btn-primary" ng-click='addPost()'>Post</button>
</form>
</div>
</div>