Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基于其他输入字段填充一个输入字段_Javascript_Html_Angularjs - Fatal编程技术网

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

Javascript 基于其他输入字段填充一个输入字段,javascript,html,angularjs,Javascript,Html,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</la

我在我的项目中使用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>
我的控制器目前仅使用一个型号$scope.f1

我想基于字段1预填充字段2。但是因为我用的是同一个模型 如果我在字段2中写入内容,则会覆盖字段1

我不想要这种行为。我应该能够在以后编辑字段2而不影响字段1

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

您可以使用,它注册一个监听器回调,以便在watchExpression更改时执行

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

您可以使用,它注册一个监听器回调,以便在watchExpression更改时执行

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

优雅!但需要注意的是,第二个字段绑定到另一个变量f2@Cherniv,请看演示,我希望它能处理您的问题concern@Satpal . 非常感谢$手表几乎解决了我所有的问题。我在那里玩代码。感谢发布演示,非常有用:优雅!但需要注意的是,第二个字段绑定到另一个变量f2@Cherniv,请看演示,我希望它能处理您的问题concern@Satpal . 非常感谢$手表几乎解决了我所有的问题。我在那里玩代码。感谢您发布演示,非常有用: