【问题标题】:Watch multiple attributes not working Angular js观看多个属性不起作用Angular js
【发布时间】:2018-08-29 16:13:49
【问题描述】:

我想根据页面上两个字段的组合自动填充日期字段。 主页是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】:

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

    $scope.$watch('vm.institution.employees', function(newValue, oldValue) {
      if (newValue.occupation != oldValue.occupation || newValue.salary != oldValue.salary) {
        vm.xx = 'test';
      }
    });
    

    【讨论】:

    • 不工作,每当我改变现有/新员工的职业或薪水时,日期值都应该改变。机构--> 员工关系是一对多的,我在机构页面中添加了员工按钮,请您检查我的工作代码以获取单个属性,我是否缺少观看 2 的代码中的任何语法?
    猜你喜欢
    • 2019-12-20
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多