填充基于其它一个输入字段 [英] Populate one input field based on other

查看:115
本文介绍了填充基于其它一个输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using angular.js in my project. In the template I have :

 <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

我要pre-填充字段2 根据字段1 。但是,由于我使用的是相同的模型
如果我写在字段2东西重写字段1

I want to pre-populate Field 2 based on Field 1. But since I am using the same model if I write something in Field 2 overwrites Field 1.

我不希望这种行为。我应该能够字段2 以后编辑,而不影响字段1

I don't want this behavior. I should be able to edit Field 2 later without affecting Field 1.

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

Is there a way to achieve this behavior ?

推荐答案

您可以使用 $看,它会注册一个侦听器回调要执行每当watchEx pression变化。

You can use $watch, it registers a listener callback to be executed whenever the watchExpression changes.

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

<大骨节病>
工作演示

这篇关于填充基于其它一个输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆