Thinkster.io第3章问题

时间:2014-08-01 16:03:46

标签: angularjs

我正在尝试使用本教程:

http://www.thinkster.io/angularjs/S2MxGFCO0B/3-communicating-with-a-server-using-a-service-and-resource

我从头开始3次。 (删除了整个Linux VM)我似乎无法获取此代码来删除firebase中的内容。它可以完美地保存到firebase,我可以看到它,但没有显示任何内容。

代码:

/app/views/posts.html:

div ng-repeat="(postId, post) in posts">
<a href="{{post.url}}">{{post.title}}</a>
<a ng-click="deletePost(postId)">delete</a>
</div>


<form ng-submit="submitPost()">
<input type ="text" ng-model="post.title" />
<input type ="text" ng-model="post.url" />
<input type ="submit" value="Add Post" />
</form>

/app/scripts/controllers/posts.js:

'use strict'; 

app.controller('PostsCtrl', function($scope, Post) {
$scope.posts = [];
$scope.post = {url: 'http://', title: ''};

$scope.submitPost = function (){
    Post.save($scope.post, function (ref) {
        $scope.posts[ref.name] = $scope.post;
        $scope.post = {url: 'http://', title: ''};
    });
    $scope.post = {url: 'http://', title: ''};


};

$scope.deletePost = function (postId) {
    Post.delete({id: postId}, function (){
        delete $scope.posts[postId];
    });
};

});

/app/scripts/services/post.js:

'use strict';

app.factory('Post', function ($resource){
    return $resource('https://xxxxxxxxx.firebaseio.com/posts/:id.json');
});

我相信这个问题出现在这3个文件中。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

在您的代码中使用类似文字对象的数组,ng-repeat指令无法显示结果。

数组索引必须是整数,并使用字符串:

Post.save($scope.post, function (ref) {
    $scope.posts[ref.name] = $scope.post;

尝试类似的东西

$scope.posts.push($scope.post);

或者只是将$ scope.posts变量声明为文字对象

$scope.posts = {};

希望它能帮到你