Angularjs $watch and $on 2 types of monitoring differences?

Source: Internet
Author: User

1. $watch Simple to use

$watch is a scope function that listens for model changes and notifies you when the part of your model changes.

$watch (watchexpression, Listener, objectequality);

The description of each parameter is as follows:

    1. Watchexpression: The Listener object, which can be a angular expression such as ' name ', or function such as functions () {return $scope. Name}.

    2. Listener: The function or expression that will be called when watchexpression changes, it receives 3 parameters: NewValue (new value), OldValue (old value), scope (scope reference)

    3. Objectequality: If the depth listener is set to true, it tells angular to check for changes in each property in the monitored object. If you want to monitor an individual element of an array or an object's properties instead of a normal value, you should use it

$watch can monitor changes in multiple models

<body ng-app= "App" ng-controller= "First" >        <button ng-click= "Name= ' a '" >1</button>        < Button ng-click= "name= ' B '" >2</button>        <button ng-click= "name= ' C '" >3</button>        < Button ng-click= "type=2" >4</button>        <button ng-click= "type=3" >5</button>        <p>{ {Name}} </p>    </body>    <script type= "Text/javascript" >        var app = Angular.module ("App", []);        App.controller ("First", function ($scope) {            $scope. name = ' Q ';            $scope. Type = 1;            function Te () {                console.log ($scope. name+ "" + $scope. Type);            }            $scope. $watch ('name+type', function (NewValue, oldValue) {                 //2 parameters are one for the new value, one for the old value                te ();            });        })    </script>

 

2. Usage of $on

$on is a scope function that listens for changes in events and notifies you when your event occurs.

Module.controller (' Freefedctrl ', [' $scope ', function ($scope) {         //Listen for Directiveclick event         $scope. $on (' Directiveclick ', function (event,param) {               console.log (param);    Print result {title: ' I am from the instruction sub-scope '}         });         $scope. Change = function (title) {              var result = ' Please be aware of receiving parent broadcasts ';              Broadcasts a Parentbroadcast event $scope to a child scope              . $broadcast (' Parentbroadcast ', {msg:result});         };  }]);

  

Angularjs $watch and $on 2 types of monitoring differences?

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.