我是Angular的新手,所以我預測道歉是一個簡單的問題。我正在嘗試爲文章創建評論系統。我有兩個角度控制器,一個用於在頁面加載時加載註釋,另一個用於向服務器提交新評論。這些工作正常,但在success()
方法中,我想更新顯示的註釋以顯示新評論。然而,我目前的代碼不起作用,我試過的任何方法似乎都不能解決它。我能得到一些幫助嗎?!
我知道這可能與不同的$scope
變量有關,但我讀過的所有文檔似乎都沒有說明這一點。
article.js
// create app
var articleApp = angular.module('articleApp', ['btford.markdown', 'ngSanitize']);
// create controller
articleApp.controller('DisplayCommentsCtrl', function ($scope, $http) {
$scope.loadComments = function() {
$http.get(Routing.generate('article_comments', { id: window.articleId })).success(function (data) {
$scope.comments = data.comments;
});
};
$scope.loadComments();
});
articleApp.controller('SubmitCommentCtrl', function ($scope, $http, $route) {
$scope.loadComments = function() {
$http.get(Routing.generate('article_comments', { id: window.articleId })).success(function (data) {
$scope.comments = data.comments;
});
};
$scope.loadComments();
$scope.formData = {
'comment':{
'save' : 'Save',
'comment' : '',
'_token' : $('#comment__token').val()
}
};
$scope.processForm = function ($route) {
$http({
method : 'POST',
url : Routing.generate('article_new_comment', { id: window.articleId }),
data : $.param($scope.formData),
headers : {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.success(function (data, $route) {
$route.reload();
});
};
});
article.html.twig
<div class="col-md-12">
<div class="commentFormContainer" ng-controller="SubmitCommentCtrl">
{% verbatim %}
<p>{{ formData.comment.comment }}/{{ formData.comment._token }}</p>
{% endverbatim %}
<!--{{ form_start(commentForm, { 'attr': { 'id': 'commentForm', 'ng-submit':'processForm()' }}) }} -->
<form name="comment" id="commentForm" ng-submit="processForm()">
{{ form_errors(commentForm) }}
{{ form_row(commentForm.comment, { 'attr': { 'ng-model': 'formData.comment.comment' } }) }}
{{ form_widget(commentForm._token) }}
{{ form_end(commentForm) }}
</div>
{% verbatim %}
<div class="articleCommentContainer" ng-controller="DisplayCommentsCtrl">
<div ng-repeat="comment in comments | orderBy: '-time'">
<div class="articleCommentComment" ng-bind-html="comment.commentHTML">
</div>
<div class="articleCommentDetails">
<p>[{{ comment.creator }} @ {{ comment.time|date:'EEE d MMM, h.mm a' }}]</p>
</div>
</div>
</div>
{% endverbatim %}
</div>
,就可以實現這個採用了棱角分明的廣播事件。 http://stackoverflow.com/questions/14502006/working-with-scope-emit-and-on –
[看看這裏](http://stackoverflow.com/questions/12008908/how-cani-i- pass-variables-between-controllers) – ODelibalta
Try $ window.location.reload();或使用廣播或從提交控制器發出並從顯示控制器 – kumaro