discountry
discountry

Reputation: 53

How to sync ng-model data after $(input).val('') to $scope

For example, Then I reset the input value via jQuery like: $('#example').val(''); But the value of angular model "Something" won't change. How to solve this trouble pleases!!!

Upvotes: 0

Views: 1241

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136174

If you're using Angular, then do it in Angular way.

I believe you shouldn't use jQuery to bind AngularJS scope variable. On input element you must use ng-model directive that will take care about angular two way binding.

HTML

<div ng-app="app" ng-controller="mainCtrl">
  <input id="sampleInput" ng-model="testInput"/> 
  Below you can get updated value of scope variable.
  {{testInput}}
  <button ng-click="click()"></button>
</div>

JS CODE

angular.module('app',[])
.controller('mainCtrl', [ '$scope', function($scope) {
   $scope.click = function(){
      console.log($scope.testInput); //here is updated scope variable inside controller 
   };
}]);

Thanks.

Upvotes: 1

Related Questions