Angularjs using $scope. $watch Monitor the change of the object model

Source: Internet
Author: User

If the object model changes, you can use $scope. $watch monitor changes

<HTMLNg-app= "myApp"><Head><title>Angularjs-demo</title><Scripttype= "text/javascript"src= "lib/angularjs/angular.min.js"CharSet= "utf-8"></Script></Head><BodyNg-controller= "ctrl"><inputtype= "text"Ng-model= "text1"ng-model-options= "{updateon: ' Blur '}" /><Div>Number of input box contents changed: {{count}}</Div></Body><Scripttype= "text/javascript">varapp=Angular.module ('myApp', []). Controller ('Ctrl', function($scope) {$scope. Count= 0; $scope. $watch ('Text1',function() {$scope. Count++; });});</Script></HTML>

Change it a little bit, output the content before and after the Modification:

<HTMLNg-app= "myApp"><Head><title>Angularjs-demo</title><Scripttype= "text/javascript"src= "lib/angularjs/angular.min.js"CharSet= "utf-8"></Script></Head><BodyNg-controller= "ctrl"><inputtype= "text"Ng-model= "text1"ng-model-options= "{updateon: ' Blur '}" /><Div>Number of input box contents changed: {{count}}</Div><Div>Original Content: {{content1}}</Div><Div>New Content: {{content2}}</Div></Body><Scripttype= "text/javascript">varapp=Angular.module ('myApp', []). Controller ('Ctrl', function($scope) {$scope. Count= 0; $scope. $watch ('Text1',function(newvalue,oldvalue) {$scope. Count++; $scope. Content1=oldValue; $scope. Content2=newvalue; });});</Script></HTML>

Angularjs using $scope. $watch Monitor the change of the object model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.