1

angular.js在我的项目中使用。在我的模板中:

 <div class='form-group'>
   <label>Field 1</label>
  <input type='text' ng-model='f1' required class="form-control">
 </div>


 <div class='form-group'>
    <label>Field 2</label>
     <input type='text' ng-model='f1' required class="form-control">
 </div>

controller现在只使用一个模型$scope.f1

我想Field 2基于Field 1. 但是因为如果我在Field 2overwrites中写一些东西,我使用的是相同的模型Field 1

我不想要这种行为。我以后应该可以在Field 2不影响Field 1.

有没有办法实现这种行为?

4

1 回答 1

5

您可以使用$watch,它会注册一个侦听器回调,以便在 watchExpression 更改时执行。

$scope.$watch(function () {
    return $scope.f1;
},
function (newValue, oldValue) {
    $scope.f2 = $scope.f1;
}, true);

Working Demo

于 2013-10-09T08:22:07.640 回答