观察多个属性不起作用的Angular js

问题描述 投票:1回答:1

我想根据页面上两个字段的组合自动填充日期字段。主页是Institution,在这个主页面我有一个员工子部分。我想看看子节中的2个字段,我尝试了这个代码,但它无法正常工作。

      $scope.$watch(function () { return vm.institution.employees.length }, function () {
        _.forEach(vm.institution.employees, function (item, index) {
            $scope.$watch(vm.institution.employees[index].occupation,vm.institution.employees[index].salary, function (newValue, oldValue) {
                if (newValue[0] != oldValue[0]) {
                    vm.xx = 'test';
                }
            },true);

        });
    });

在这个相同的机构页面员工子部分我正在使用下面的代码观看单个属性它正常工作,但看着2不起作用。

    $scope.$watch(function () { return vm.institution.employees.length }, function () {
        _.forEach(vm.institution.employees, function (item, index) {
            debugger;
            item.employeeDesignation = _.filter(vm.desginations, { id: item.designationTypeId });
            $scope.$watch(function () { return vm.institution.employees[index].designationTypeId }, function (newValue, oldValue) {
                if (newValue != oldValue) {
                    item.employeeDesignation = _.filter(vm.desginations, { id: newvalue });
                           }
            });

        });
    });
javascript .net angularjs angularjs-scope
1个回答
0
投票

您可以使用$ watchGroup观看一系列表达式。但在您的情况下,您可以只观看vm.institution.employees并将其他逻辑移至回调。

$scope.$watch('vm.institution.employees', function(newValue, oldValue) {
  if (newValue.occupation != oldValue.occupation || newValue.salary != oldValue.salary) {
    vm.xx = 'test';
  }
});
© www.soinside.com 2019 - 2024. All rights reserved.