gpt4 book ai didi

javascript - Angular 教程 - 我在哪里迷路了?

转载 作者:行者123 更新时间:2023-11-29 21:50:04 24 4
gpt4 key购买 nike

因此,我正在关注位于 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

最佳答案

请从 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>

关于javascript - Angular 教程 - 我在哪里迷路了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29734752/

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