2015-04-04 83 views
0

我想显示的帖子表本教程如下:从火力地堡与显示数据的角度

http://code.tutsplus.com/tutorials/creating-a-web-app-from-scratch-using-angularjs-and-firebase--cms-22391

5部分仍未公布,所以我把教程一路部分4并试图建立一个post.html视图。然而,作为角度和火力基础的初学者,我真的很挣扎。

这里是我的岗位/ post.js文件中的代码:

'use strict'; 

angular.module('myApp.post', ['ngRoute']) 

.config(['$routeProvider', function($routeProvider) { 
$routeProvider.when('/post', { 
templateUrl: 'post/post.html', 
controller: 'PostCtrl' 
}); 
}]) 



.controller('PostCtrl', ['$scope','$firebase' ,  function($scope,$firebase) { 
$scope.message = 'Hello World'; 
$scope.viewPost = function(){ 
    $scope.article.title = ""; 
    $scope.article.post = ""; 
    $scope.article= {}; 
    $scope.myData = new Firebase("https://glowing-fire-6133.firebaseio.com/Posts"); 
}; 

$scope.myData.on('value', function(snapshot){ 
    $scope.article = snapshot.val(); 

    }); 
}]); 

这里是我的post.html文件的代码

<title></title> 

<link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet"> 

<link href="assets/css/blog.css" rel="stylesheet"> 


</head> 


<body ng-controller="PostCtrl"> 
<h1>{{message}}</h1> 

    <div ng-repeat="post in posts"> 
     <h2>{{article.title}}</h2> 
     <div>{{article.post}}</div> 
    </div> 

</body> 

</html> 

我从此没有任何错误或结果。我确信添加

'myApp.post', //Post view 

模块app.js,并在index.html的,但仍然没有在

我在做或失去一些真正愚蠢的东西吗? I.E.登录到数据库等?

感谢您的耐心

## Addpost脚本
'use strict'; 

angular.module('myApp.addPost', ['ngRoute']) 

.config(['$routeProvider', function($routeProvider) { 
$routeProvider.when('/addPost', { 
templateUrl: 'addPost/addPost.html', 
controller: 'AddPostCtrl' 
}); 
}]) 

.controller('AddPostCtrl',  ['$scope','$firebase',function($scope,$firebase) { 
$scope.AddPost = function(){ 
var title = $scope.article.title; 
    var post = $scope.article.post; 

var firebaseObj = new Firebase("https://glowing-fire-6133.firebaseio.com/Posts"); 
    var fb = $firebase(firebaseObj); 

fb.$push({ title: title,post: post}).then(function(ref) { 
    console.log(ref); 
}, function(error) { 
    console.log("Error:", error); 
}); 

} 
}]); 

回答

0

哪里$scope.posts?我没有看到你的控制器,所以没有什么可以循环。

你应该有$scope.posts = [/* a bunch of post objects*/];

我其实还挺看起来像你应该做<div ng-repeat="post in myData">

+0

感谢您的回复。我理解你的观点,但是如何让myData成为一堆$ scope.posts对象?我需要按照myData.items做些什么吗? – HGB 2015-04-05 18:38:23

+0

@Cigana它看起来像myData是一堆post对象给我。不是吗?无可否认,我不知道Firebase,但是这个url看起来像一个返回post对象的控制器的请求。你能展示你从中得到的JSON吗? – Yatrix 2015-04-06 01:30:57

+0

是的,你是正确的,这是一堆对象,我只是不明白怎么能在一个对象中请求它。 firebase格式类似于Json的对象的方式如下:'glowing-fire-6133'(db name) 'Jm9nJDRrIq9Dncfj1gv'(表I相信) 'post:post content' 'title:post title' – HGB 2015-04-06 20:25:24