2

So lets say I want a create a custom component in Angular 2+ for the following class

public class user {
    name: string;
    id: number;
    address: string;
}

I've created a custom component

<input [(ngModel)]="name" />
<input [(ngModel)]="id" />
<input [(ngModel)]="address" />

I've added this custom component into my main page

<user-component></user-component>

What I want to do now is pass the User class as ngModel inside my custom component and bind it with public user:User variable in my main page class. Something like this

<user-component [(ngModel)]="user"></user-component>

I've found few examples of using ControlValueAccessor, NG_VALUE_ACCESSOR but all these examples work with single component like one text input. Is there a way to make it work for multiple inputs inside the custom component as well?

1
  • you should use reactive forms Commented Sep 11, 2019 at 7:03

2 Answers 2

7

The way to achieve this is to use @Input on the UserComponent

export class UserComponent {
  @Input() user: User;
}

Then binding to the input you declared in your parent template

<user-component [user]="user"></user-component>

Now this is only one way, input to the UserComponent,

To make it two ways like ngModel works you would need to add the output EventEmiter As follows

export class UserComponent {
  @Input() user: User;
  @Output('userChange') emitter: EventEmitter<User> = new EventEmitter<User>();
}

Note that the userChange keyword is important here to make it work.

Only then you would be able to write the two way binding

<user-component [(user)]="user"></user-component>

Adding a bit of reading docs here angular.io/guide/template-syntax#property-binding-property

Also with just a google search away, i was able to find this also good SO answer on the same topic custom-input-and-output-on-same-name-in-angular2-2-way-binding

Sign up to request clarification or add additional context in comments.

5 Comments

As everyone is pointing out, a nice life saver would be to use angular.io/guide/reactive-forms#grouping-form-controls a FormGroup with FormControls inside
If I use reactive forms, will I get two way data binding ?
Reactive forms help to manage values and validity of the inputs. For example you can subscribe to a FormControls change events. You still need the input outputs on the component to notify the parent component.
@Simplicity's_Strength, is an object, it's NOT necesary @Output, see e.g. stackblitz.com/edit/…
@Eliseo good point, this two way binding example is not needed in case you can bind to the Object directy. Your answer and stackblitz are valids for OPs initial need.
1

as you has an object you can simple pass the object as @Input and

<user-component [user]="user"></user-component>

And your child

@Input() user:any

<input [(ngModel)]="user.name" />
<input [(ngModel)]="user.id" />
<input [(ngModel)]="user.address" />

NOTE: Really I love ReactiveForms, cheer up to use these

1 Comment

Yes I can pass the object, but once user updates the input fields inside custom component, will I get updated filed values in main component ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.