如何以角度形式将地址或任何字段复制到另一个字段?

时间:2015-06-13 05:01:09

标签: javascript angularjs angularjs-directive angular-formly

我正在试图找出当用户点击复选框时如何将主电子邮件复制到备用电子邮件。我正在使用角度的angular-formly指令。我坚持这个小实现。

以下是Plunkr

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

最简单的方法是在复选框上添加一个手表,然后将备用电子邮件设置为电子邮件地址。

$scope.formFields = [
  {
    "key": "firstName",
    "type": "text",
    "label": "First Name",
    "placeholder": "Jane",
    "required":true
},{
    "key": "email",
    "type": "email",
    "label" :"Primary Email",
    "placeholder": "janedoe@gmail.com",
    "required":true
},
{
    "key": "altEmail",  // you need a unique key for this one
    "type": "email",
    "label":"Alternate Email",
    "placeholder": "janedoe@gmail.com",
    "required":true,
    ngModelAttrs: {
      myCustomValue: {
        bound: 'email',
        attribute: 'email'
      }
    },
    templateOptions: {
      myCustomValue: "email"
    }
},
// ...

$scope.$watch('result.sameAsPrimary', function(newValue) {
  if (newValue) {
    $scope.result.altEmail = $scope.result.email;
  }
});