简体   繁体   English

如何使用angularjs观看多个属性更改

[英]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。$ watchGroup ,这是angular.js 1.3中的新功能(在angular 1.2中使用$ 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. newValues和oldValues是数组,并且indecies对应于$ watchGroup的第一个参数中的属性索引。

See fiddle: https://jsfiddle.net/7je6r2mn/1/ 参见小提琴: https//jsfiddle.net/7je6r2mn/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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