基于其他填充一个输入字段

Dee*_*eyi 1 javascript html5 angularjs

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>
Run Code Online (Sandbox Code Playgroud)

controller现在只使用一种型号$scope.f1

我想Field 2基于预先填充Field 1.但是,如果我在Field 2覆盖中写一些东西,我会使用相同的模型Field 1.

我不想要这种行为.我应该能够在Field 2不影响的情况下进行编辑Field 1.

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

Sat*_*pal 5

您可以使用$ watch,它会在watchExpression更改时注册要执行的侦听器回调.

$scope.$watch(function () {
    return $scope.f1;
},
function (newValue, oldValue) {
    $scope.f2 = $scope.f1;
}, true);
Run Code Online (Sandbox Code Playgroud)

Working Demo