2015-04-04 88 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