如何使$ watch在ng-model的不同领域工作

时间:2017-09-19 19:10:01

标签: angularjs watch

在我的html页面上,我有5 6个不同的表格

    <input type="text" class="form-control" ng-model="edit.TIME"placeholder="Time" maxlength="30" ng-required="true" />
    <input type="text" class="form-control" ng-model="edit.NAME" placeholder="Time" maxlength="30" ng-required="true" />
    <input type="text" class="form-control" ng-model="edit.Place"placeholder="Time" maxlength="30" ng-required="true" />
    <input type="text" class="form-control" ng-model="edit.LASTNAME" placeholder="Time" maxlength="30" ng-required="true" />

如何使用$ watch调用每个ng-model。如果有任何改变,$ watch将显示更改值。在我的控制器端

    $scope.old.NAME="ABC";   
    $scope.$watch('edit', function (Value) {          
        if ($scope.old.NAME != $scope.edit.NAME) {  
            $scope.old.NAME.append($scope.edit.NAME)
        }

1 个答案:

答案 0 :(得分:1)

使用true作为第三个参数调用watch:

$scope.$watch('edit', function(newVal, oldVal){
    console.log(newVal);
}, true);