Javascript AngularJS:暂时禁用数据绑定。
是否可以暂时禁用AngularJS中的数据绑定。我有一个使用Javascript AngularJS:暂时禁用数据绑定。,javascript,angularjs,Javascript,Angularjs,是否可以暂时禁用AngularJS中的数据绑定。我有一个使用ng repeat渲染的列表项。我想对项执行一些列表操作,同时暂时禁用数据绑定,以便中间更改不会反映在页面上,从而降低页面速度。相反,我希望对列表执行操作,然后重新启用数据绑定 对于单个变量,可以通过保留原始值的单独副本并在变量更改时重置该变量来临时解除绑定,如果该变量不是只读的,则在其变为只读时重置副本 var items = angular.copy($scope.items); .. .. .. /* perform operat
ng repeat
渲染的列表项。我想对项执行一些列表操作,同时暂时禁用数据绑定,以便中间更改不会反映在页面上,从而降低页面速度。相反,我希望对列表执行操作,然后重新启用数据绑定 对于单个变量,可以通过保留原始值的单独副本并在变量更改时重置该变量来临时解除绑定,如果该变量不是只读的,则在其变为只读时重置副本
var items = angular.copy($scope.items);
..
..
..
/* perform operations over items */
..
..
..
$scope.items = angular.copy(items);
js
html
-是只读的
复选框已选中
复选框未选中
只读?
如果您的情况可行,您可以始终在项的副本上执行这些列表操作。更多的上下文可能会有所帮助。模型只会在执行操作的代码运行后更新。您可能需要描述如何操作以获得更有用的东西。
function ViewCtrl($scope) {
$scope.modelValue = {pointer: false};
$scope.modelReadonly = {pointer: true};
var modelValueStatic = undefined;
$scope.$watch('modelValue.pointer',function (newVal, oldVal) {
console.log('newval: '+newVal+' oldVal:'+oldVal+' static:'+modelValueStatic);
if (newVal != oldVal && $scope.modelReadonly.pointer && modelValueStatic != undefined) {
$scope.modelValue.pointer = modelValueStatic;
}
if (!$scope.modelReadonly.pointer || newVal == oldVal) {
modelValueStatic = $scope.modelValue.pointer;
};
});
}
<div ng-app>
<div ng-controller="ViewCtrl">
<input type="checkbox" ng-model="modelValue.pointer" ng-readonly="modelReadonly" /><span ng-show="modelReadonly.pointer"> <- is read-only</span><br/>
<span ng-show="modelValue.pointer">checkbox IS CHECKED</span>
<span ng-show="!modelValue.pointer">checkbox IS NOT CHECKED</span>
<div>
<input type="checkbox" ng-model="modelReadonly.pointer" /> readonly?
</div>
</div>
</div>