最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular directive didn't update model view - Stack Overflow

programmeradmin6浏览0评论

I'm trying to show ments in template after clicking button in form using directive

HTML:

<h2>Comments</h2>
<ul class="ments_list">
    <li ng-repeat=" in ments" ng-cloak>{{.name}} wrote<div class="message">{{.text}}</div></li>
</ul>
<div class="add_ment" 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-ment ng-model="addComm">Add</button>
</div>

And 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);
                $scopements.push(angular.copy(ngModel.$modelValue));
            });
        }
    }
});

But after clicking "Add" in HTML my view didn't update. If I refresh the page (I'm using ngStorage) - new ment will appear in list, but not after clicking "Add" button.

I'm trying to show ments in template after clicking button in form using directive

HTML:

<h2>Comments</h2>
<ul class="ments_list">
    <li ng-repeat=" in ments" ng-cloak>{{.name}} wrote<div class="message">{{.text}}</div></li>
</ul>
<div class="add_ment" 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-ment ng-model="addComm">Add</button>
</div>

And 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.ments.push(angular.copy(ngModel.$modelValue));
            });
        }
    }
});

But after clicking "Add" in HTML my view didn't update. If I refresh the page (I'm using ngStorage) - new ment will appear in list, but not after clicking "Add" button.

Share Improve this question edited Apr 20, 2016 at 23:58 angelcool 2,5461 gold badge25 silver badges26 bronze badges asked Apr 20, 2016 at 21:06 Demyd GanenkoDemyd Ganenko 1081 silver badge9 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

It's happening because you are changing the value of your $scope variable inside javascript click handler. Try this:

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.$apply(function() {
                     $scope.ments.push(angular.copy(ngModel.$modelValue));
               });
            });
        }
    }
});

You shall notify angular that something has changed:

element.on("click", function(event){
    event.preventDefault();
    console.log(ngModel.$modelValue);
    $scope.$apply(function () {
        $scope.ments.push(angular.copy(ngModel.$modelValue));
    });
});

You don't need the ngModelCtrl since the value is attached to scope already.

But what cause the problem is that you don't notify angular that the model has change, in order to do so, just call $scope.$apply

When you change model in asynchronous callback, you should encapsulate it into $apply:

$scope.$apply(function(){
    $scope.variable = true;
});

The alternative solution is to call $apply directly, after change:

$scope.variable = true;
$scope.$apply();
发布评论

评论列表(0)

  1. 暂无评论