的提示
我一直在关注最近修订的Thinkster.ioAngularJS教程:学习构建实时Web应用程序,但我被困在80%左右,特别是删除帖子的代码。由于某些原因,我无法删除我使用教程提供的以下删除功能创建的任何帖子。
delete: function (post) {
if (User.signedIn()){
var user = User.getCurrent();
if (user.username === post.owner) {
posts.$remove(post).then(function () {
User.posts(user.username).$remove(post.$id);
});
}
}
}
当我在if块中追赶时
console.log(post);
我得到了字符串
-JWZaIrYaPYNls95jQAw
因此,由于post
是一个字符串,下面的3行永远不能工作
if (user.username === post.owner) {
posts.$remove(post).then(function () {
User.posts(user.username).$remove(post.$id);
只有当post
是一个对象时,它们才能工作。关于如何让这个删除功能正常工作,有什么想法吗?
文件
user.js工厂
'use strict';
app.factory('User', function ($firebase, FIREBASE_URL, $rootScope, $log) {
var ref = new Firebase(FIREBASE_URL + 'users');
var users = $firebase(ref);
function setCurrentUser(username) {
$rootScope.currentUser = User.findByUsername(username);
}
$rootScope.$on('$firebaseSimpleLogin:login', function (event, authUser) {
var query = $firebase(ref.startAt(authUser.uid)
.endAt(authUser.uid)).$asArray();
query.$loaded(function () {
setCurrentUser(query[0].username);
});
});
$rootScope.$on('$firebaseSimpleLogin:logout', function() {
delete $rootScope.currentUser;
});
var User = {
create: function (authUser, username) {
var user = $firebase(ref.child(username)).$asObject();
return user.$loaded(function() {
user.username = username;
/*jshint camelcase: false */
user.md5_hash = authUser.md5_hash;
user.$priority = authUser.uid;
user.$save();
});
$log.debug(users);
},
findByUsername: function (username) {
if (username) {
return $firebase(ref.child(username)).$asObject();
}
},
getCurrent: function () {
return $rootScope.currentUser;
},
signedIn: function () {
return $rootScope.currentUser !== undefined;
},
posts: function(username) {
return $firebase(new Firebase(FIREBASE_URL + 'user_posts/' + username));
}
};
return User;
});
post.js工厂
'use strict';
app.factory('Post', function($firebase, FIREBASE_URL, User){
var ref = new Firebase(FIREBASE_URL + 'posts');
var posts = $firebase(ref).$asArray();
var Post = {
all: posts,
create: function(post) {
if(User.signedIn()) {
var user = User.getCurrent();
post.owner = user.username;
return posts.$add(post).then(function(ref) {
var postId = ref.name();
User.posts(user.username).$set(postId, postId);
return postId;
});
}
},
find: function(postId) {
return $firebase(ref.child(postId)).$asObject();
},
delete: function (post) {
if (User.signedIn()){
var user = User.getCurrent();
if (user.username === post.owner) {
posts.$remove(post).then(function () {
User.posts(user.username).$remove(post.$id);
});
}
}
}
};
return Post;
});
posts.js控制器
'use strict';
app.controller('PostsCtrl', function($scope, $location, Post) {
$scope.posts = Post.all;
$scope.post = {url: 'http://', title: ''};
$scope.deletePost = function(post) {
Post.delete(post);
};
});
posts.html查看
<div class="container posts-page">
<div class="post row" ng-repeat="(postId, post) in posts">
<div class="col-xs-1">
</div>
<div class="col-md-9 col-xs-11">
<div class="info">
<a href="{{ post.url }}">
{{ post.title }}
<span class="url">({{post.url | hostnameFromUrl}})</span>
</a>
</div>
<div>
<span>{{ post.score || 0 }} votes</span>
—
<span> submitted by
<a href="#/user/{{post.owner}}" >{{ post.owner }}</a>
</span>
—
<a href="#/posts/{{ post.$id }}">comments</a>
<a ng-click="deletePost(post.$id)"
ng-show="signedIn() && post.owner === currentUser.username">delete</a>
</div>
</div>
<div class="col-md-2">
</div>
</div>
</div>
看看这个:
<a ng-click="deletePost(post.$id)" ...>delete</a>
你在推杆$id,您需要使用object post。像这样:
<a ng-click="deletePost(post)" ..>delete</a>
我还没有尝试更新的教程,感谢