我試圖用點擊按鈕的形式指令角指令沒有更新
HTML後顯示在模板評論模型視圖:
<h2>Comments</h2>
<ul class="comments_list">
<li ng-repeat="com in comments" ng-cloak>{{com.name}} wrote<div class="message">{{com.text}}</div></li>
</ul>
<div class="add_comment" ng-show="posts.length > 0">
<input type="text" class="form-control" ng-model="addComm.name" placeholder="Your name">
<textarea class="form-control" ng-model="addComm.text" placeholder="Enter message"></textarea>
<button class="btn btn-success" add-comment ng-model="addComm">Add</button>
</div>
和JS:
app.directive('addComment', function() {
return {
restrict: 'A',
require: 'ngModel',
priority: 1,
link: function ($scope, element, attrs, ngModel) {
element.on("click", function(event){
event.preventDefault();
console.log(ngModel.$modelValue);
$scope.comments.push(angular.copy(ngModel.$modelValue));
});
}
}
});
但在點擊HTML中的「添加」後,我的視圖沒有更新。如果我刷新頁面(我正在使用ngStorage) - 新評論將出現在列表中,但不會在點擊「添加」按鈕後出現。