Angular 2, passing full object as parameter
Yes it is totally fine to pass the entire object as a property.
The syntax is the same, so just create a property for the entire object.
@Component({ selector: 'my-component'})export class MyComponent{ @Input() item;}<my-component [item]=item></my-component>
Here is an example: http://www.syntaxsuccess.com/viewarticle/recursive-treeview-in-angular-2.0
There is no problem on doing it. You can choose both syntaxes:
@Component({ selector: 'my-component', inputs: ['item: item']})export class TodoItem { item: { title: string, state: boolean };}
or
@Component({ selector: 'my-component'})export class TodoItem { @Input() item: { title: string, state: boolean };}
and the binding:
<todo-item [item]="item" *ng-for="#item of list"></todo-item>
Something you need to have in mind though, is that when passing an object this way, you are passing a reference to the object. This means that any change you make to the object in the "child" Component, will be reflected in the "parent" Component object:
export class TodoItem implements OnInit { ngOnInit() { //This is modifying the object in "parent" Component, //as "this.item" holds a reference to the same "parent" object this.item.title = "Modified title"; }}
The exception to this is if you assign a different object. In that case it won't reflect in "parent" Component, as it is no longer the same object reference:
export class TodoItem implements OnInit { ngOnInit() { //This will not modify the object in "parent" Component, //as "this.item" is no longer holding the same object reference as the parent this.item = { title: 'My new title', state: false }; }}