在范围变量更新后更新JSON属性

时间:2015-07-27 10:33:52

标签: angularjs angularjs-scope

我的控件中有以下JSON数组

$scope.companyExcelColumns = [
  {
    label: 'Name', checked: true, disabled: true, id:'nameExcel'
  },
  {
    label: 'value', checked: true, disabled: $scope.records > 1000 ? true : false, id: 'valueExcel'
  }
];

最初$ scope.records是0,稍后它正在改变但禁用的属性没有更新,是否有解决方法?

由于

2 个答案:

答案 0 :(得分:0)

这不是一个常见的角度问题,而是一个编程问题。当然,残疾人的价值不会改变。它计算一次并完成。

您可以在$ scope.records对象上使用$ watch并观察更改。当此对象更改时,您可以重新分配已禁用的值。

$scope.$watch('records', function(){ disabled = $scope.records.length > 1000})

答案 1 :(得分:0)

使用$watch方法观看$scope.records的每次更改:

$scope.companyExcelColumns = [
  {
    label: 'Name', checked: true, disabled: true, id:'nameExcel'
  },
  {
    label: 'value', checked: true, disabled: $scope.records > 1000 ? true : false, id: 'valueExcel'
  }
];

$scope.$watch('records', function(newValue, oldValue){
  $scope.companyExcelColumns = [
    {
      label: 'Name', checked: true, disabled: true, id:'nameExcel'
    },
    {
      label: 'value', checked: true, disabled: newValue > 1000 ? true : false, id: 'valueExcel'
    }
  ];
})
相关问题