3gwebtrain
3gwebtrain

Reputation: 15293

how to watch the `ng-model` from the form element

I am getting data from server and binding to form elements. in case of these datas are edited by the user, i would like to know the status. for that I am trying to watch the object. but i am not getting the result.

what is the correct way to watch the change from the ng-model?

here is my try:

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

app.controller('MainCtrl', function($scope) {
  $scope.issue = {
    name:"arr",
    age:15
  };

  $scope.$watch( $scope.issue, function(nv,ov) {

    console.log(nv, ov);

  }, true)

});


 <body ng-controller="MainCtrl">

    <form action="">
      <input type="text" ng-model="issue.name">
    </form>

  </body>

Live Demo

Upvotes: 0

Views: 40

Answers (2)

Skintkingle
Skintkingle

Reputation: 1579

Simple answer you'll be happy to know. $watch already presume $scope as its point of watching. so you supply as the first paramter the string value of the variable on $scope you want to watch. If you change

$scope.$watch( $scope.issue, function(nv,ov) {

to

$scope.$watch( "issue", function(nv,ov) {

everything will work :)

Upvotes: 2

Bata
Bata

Reputation: 651

$scope.$watch('issue', function(nv,ov) {

    console.log(nv, ov);

}, true)

Upvotes: 1

Related Questions