How to copy address or any fields to another fields in angular-formly?

410 views Asked by At

I'm trying to figure out how can I copy primary email to alternate email when user click on checkbox. I'm using angular-formly directive of angular. I stuck in this small implementation.

Here is the Plunkr

Any help would be appreciated.

1

There are 1 answers

2
Steven Lambert On BEST ANSWER

Easiest way is to add a watch on the checkbox and then set the alternative email to the email address.

$scope.formFields = [
  {
    "key": "firstName",
    "type": "text",
    "label": "First Name",
    "placeholder": "Jane",
    "required":true
},{
    "key": "email",
    "type": "email",
    "label" :"Primary Email",
    "placeholder": "[email protected]",
    "required":true
},
{
    "key": "altEmail",  // you need a unique key for this one
    "type": "email",
    "label":"Alternate Email",
    "placeholder": "[email protected]",
    "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;
  }
});