我是Angular的新手,我正在嘗試設置基本的「提交評論」&「顯示評論列表」頁面。我希望在提交評論後更新評論列表。目前我可以提交,但必須手動刷新頁面才能看到它。如何在使用服務提交Angular表單後更新列表?
控制器:
app.controller('FormController', function($scope, CommentService) {
$scope.comment = {}; //will be populated by basic form
$scope.submit = function() {
return CommentService.post($scope.comment).then(function(){
$scope.comment = ""; //blanks out form on POST
});
}
});
app.controller('CommentsController' function($scope, CommentService) {
CommentService.list().then(function(comments){
$scope.comments = comments.data;
});
});
服務:
app.service('CommentService', function($http) {
this.post = function(comment) {
return $http.post('/api/v1/comments/', comment);
};
this.list = function() {
return $http.get('/api/v1/comments/').
success(function(data) {
return data;
});
};
//How do I connect the two controllers?
});
HTML表單/列表是超級通用的,我只是用 「NG重複」 來顯示評論。我在正確的軌道上嗎?有沒有簡單的我可以這樣做,當通過表單提交評論時,評論列表將被更新?
在此先感謝!
看看這個:http://stackoverflow.com/questions/9293423/can-one-controller-call-another-in-angularjs – welbornio