Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing ngModel value programatically from controller

I wish to modify value of an ngModel variable from my controller. However it doesn't seem to be reflecting in views. I have seen few other questions on SO but none worked for me. I want a solution where I do not need to create a new directive for this. I have also tried to wrap the change in $scope.$apply without success.

Here is plunkr demonstrating the issue.

Here is code from plunkr

JavaScript Controller:

    app.controller('MainCtrl', function($scope) {
  $scope.Attachment = "something"
  $scope.change = function () {
      $scope.$apply(function() {
          $scope.Attachment = "otherthing";
      });
}

HTML:

<body ng-controller="MainCtrl">
    <section class="content" ng-app="offer">
        <div>
            <button name="change" ng-click="change()" ng-model="Attachment">change</button>
            <!-- <input name="Attachment" type="file" class="file_up" onchange="angular.element(this).scope().change(this)" ng-model="Attachment" /> -->
            <span>{{Attachment}}</span>

        </div>
    </section>
</body>
like image 683
Abhishek Bansal Avatar asked Sep 05 '26 14:09

Abhishek Bansal


2 Answers

It's a best practice to bind to object properties instead of primitive types.
You are binding to a string which is a primitive type and is immutable.
You should also remove the $apply as it is not necessary since you are under the angular hood so it will perform the $apply automatically.

If you add your data as a property of an object you will not lose the reference anymore:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  //this has changed:
  $scope.data = {
    Attachment: "something"
  }

  $scope.change = function() {
    //and $apply was removed from here
    $scope.data.Attachment = "otherthing";
  }
});

and in the html you just need to change:

<span>{{data.Attachment}}</span>

EDIT: although some other answers are correct, I thought you should see what's a best practice for binding. The updated plunker here.

like image 165
bosch Avatar answered Sep 07 '26 04:09

bosch


Remove the ng-model from the button and remove the $scope.$apply from the change handler:

http://plnkr.co/edit/lOyoTBxs0L0hMs20juLS?p=preview

like image 25
sma Avatar answered Sep 07 '26 05:09

sma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!