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

I'm trying to show ments in template after clicking button in form using directiveHTML:<h2>C

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();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744124410a4559549.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信