【发布时间】:2015-07-07 08:24:14
【问题描述】:
我设法在前端显示了海报的用户 ID,我尝试了许多不同的方法来显示用户名而不是 ID。找不到问题或该怎么做。
要发布的表单
<form ng-show="$root.currentUser">
<label>Name</label>
<input ng-model="newPost.name">
<label>Description</label>
<input ng-model="newPost.description">
<button ng-click="newPost.owner=$root.currentUser._id; posts.push(newPost)">Add</button>
</form>
控制器
angular.module('posts').controller('PostsCtrl', ['$scope', '$meteor',
function($scope, $meteor){
$scope.posts = $meteor.collection(Posts);
$scope.remove = function(post){
$scope.posts.remove(post);
};
}]);
型号:
Posts = new Mongo.Collection("posts");
Posts.allow({
insert: function (userId, post) {
return userId && post.owner === userId;
},
update: function (userId, post, fields, modifier) {
return userId && post.owner === userId;
},
remove: function (userId, post) {
return userId && post.owner === userId;
}
});
编辑: html:
<p class="post-user">{{post.owner}}</p>
现在一切正常,但这次显示的是用户 ID 而不是用户名。
提前致谢!
【问题讨论】:
-
只知道这几行代码我会说几乎没有什么是正确的。 $root 在范围内定义在哪里? HTML 代码中使用的控制器在哪里?为什么要隐藏表单而不是周围的 div 或其他什么?为什么您将 post.owner 设置为用户 ID,但在您的 html 段落中使用 post.username ?也许这一切都是由流星完成的,我不知道,但我对此表示怀疑。尝试一下您的浏览器工具(在浏览器窗口中键入 F12 键;至少在 Chrome 和 Firefox 上)。如果有错误,您可以在控制台选项卡中看到它。
-
我认为 HTML 无关紧要,因为一切都像魅力一样工作,但我没有显示用户名......即:只有帖子创建者可以删除帖子。它正在工作,但不是名称。我会再次检查控制台并粘贴我的完整代码......谢谢。
-
也许你不能在
ng-click中使用$root,但必须调用一个方法并使用$rootScope。请尝试一下。有关用法示例,请参阅此问题:stackoverflow.com/questions/30693154/… -
嗨。点击有效。我刚刚尝试了 {{post.owner}},它向我显示了用户 ID。现在更新问题。
-
我能问一下你为什么同时使用 Meteor 和 AngularJS 吗?对我来说,这听起来像是一个灾难的秘诀。两者本质上是不同的。 ReactJS 是 Meteor 更好的伴侣。
标签: angularjs meteor angular-meteor