简体   繁体   中英

How to bind with component recursion in angular

I got this simple code:

@Component({
    selector: 'app-tree',
    template: `<div>{{content.name}}</div>
               <app-tree [(content)]="contentChild"
                         *ngFor="let contentChild of content.childs"></app-tree>`
})
export class TreeComponent {

@Input() content: Content;

}

but the [(content)]="contentChild" throw me the following error:

ERROR Error: Uncaught (in promise): Error: Cannot assign to a reference or variable!

How to solve this?

Found the solution:

<app-tree [(content)]="content.childs"
   *ngFor="let contentChild of content.childs"></app-tree>

You can't have two way binding, only [reference] with *ngFor however you can bind the object directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM