如何使用$scope.$watch和ng-repeat

How to use $scope.$watch and ng-repeat?

本文关键字:ng-repeat watch scope 何使用      更新时间:2023-09-26

我有工作代码:

$scope.$watch('vm.reg', function (newValue) {
  if (newValue !=  null) {
    vm.reg = newValue.replace(/'D+/, '');
  }
});
<input type="text" class="form-control" name="dest_regex" ng-model="vm.reg"/>

但是我需要使用 $watchng-repeat这样的事情:

<div ng-repeat="fi in filterElments">
    <input type="text" class="form-control" name="dest_regex" ng-model="fi.reg"/>
    <!--<input type="text" class="form-control" name="dest_regex" ng-model="fi.reg"/>
    <input type="text" class="form-control" name="dest_regex" ng-model="fi.reg"/>
     .................. -->
</div>
ng

-更改为ng-repeat中ng模型的$watch

您可以检测输入值是否有变化

$scope.replacing = function(item){
  //do somthing
}
<div ng-repeat="fi in filterElments">
    <input type="text" class="form-control" name="dest_regex" ng-model="fi.reg" ng-change="replacing(fi)"/>
</div>

对于您的具体情况,我建议查看ngModelController $parsers并$formatters。

https://docs.angularjs.org/api/ng/type/ngModel.NgModelController

https://stackoverflow.com/a/22843592/274500