Reputation: 15293
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>
Upvotes: 0
Views: 40
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
Reputation: 651
$scope.$watch('issue', function(nv,ov) {
console.log(nv, ov);
}, true)
Upvotes: 1