简体   繁体   中英

How to watch multiple property change using angularjs

I have two properties to watch for and update the third depends on change in first two. here is my code Can anyone help me on this?

return {
        context: 'prop1',
        watch: [
                {
                   property: ["prop2","prop3"], // this to watch for
                   update: [
                   {
                       dest: "prop4", // update this prop based on change in prop2 and prop3
                       src: [function (value) {
                           if (value.prop2change === 'thisvalue') { 
                               return prop3changevalue;
                           }
                           else
                               return 0;
                       },"newValue"]
                   }
             }
         ]
     }

Using $scope.$watchGroup , which is new in angular.js 1.3 ( In angular 1.2 use $watch )

   $scope.igor = 'Ershov'
   $scope.matias = 'Pachuli'

   var names = ['igor', 'matias']

   $scope.$watchGroup(names, function(newValues, oldValues) {
        // do something


      });

newValues and oldValues are arrays and indecies correspond to index of property in first parameter to $watchGroup.

See fiddle: https://jsfiddle.net/7je6r2mn/1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM