【发布时间】:2014-04-17 21:26:27
【问题描述】:
我是 Angular 的新手,我正在尝试设置一个基本的“提交评论”和“显示 cmets 列表”页面。我希望在提交评论后更新 cmets 列表。目前可以提交,但必须手动刷新页面才能看到。
控制器:
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-repeat”来显示 cmets。我在正确的轨道上吗?当通过表单提交评论时,我可以做一些简单的事情吗? cmets 列表将被更新?
提前致谢!
【问题讨论】:
标签: javascript forms angularjs frontend