$watch in angularJS
watch method is listening component, what your mention to $watch expression name. If your mention of ng-model, this model have any reaction happen from your ng-model like your declared text type this ng-model happen key press or key type means that time this $watch method take every single action from corresponding ng-model. This action take and do it your new functionality of every single action process.
$watch method main purpose to happen every single action made to logic needed to apply from that location. Like key up and down function logic validation apply logic from here.
Example for your need to apply not allow space bar from your email id field that logic problem is need to apply for $watch method.
$watch(watchExpression, listener, [objectEquality]);
watchExpression - This is for reference to ng-model name declaration
listener - Listener is make function when it action happen that time to trigger for this listener function
ObjectEquality - This comparative old action object and current object reference equal process
$watch method Example
Html:
<div ng-app="watchApp">
<div ng-controller="watchController">
<input type="text" ng-model="name" ng-trim='false'/>
</div>
</div>
Javascript :
var app = angular.module('watchApp',[]);
app.controller("watchController",['$scope','$timeout',function($scope, $timeout){
$scope.name = "";
$scope.$watch("name",function(){
$scope.name = $scope.name.toLowerCase().replace(/\s+/g,'');
});
}]);
Result:
Comments
Post a Comment