1

How does one write HTML to populate a complex @Input parameter in Angular?


If I have an Angular Component like this:

@Component({
  selector: 'app-one',
  templateUrl: './one.component.html',
  styleUrls: ['./one.component.css']
})
export class OneComponent implements OnInit {

  @Input() data: MyCustomType;
...

export class MyCustomType {
  id: string;
  value: number;
}

Can I write HTML to populate it?
Something in the lines of:

<app-my data="{'id':'a','value':1}></app-my>

There is no problem populating it through Typescript, as is the normal case, but in this case I want to populate it from HTML alone.

1 Answer 1

2

You have to use data binding syntax like so:

<app-my [data]="{'id':'a','value':1}"></app-my>

When using this syntax you can do various javascript operations, e.g. creating object, calling methods etc. Of course only with what is available on component class.

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

Comments

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.