最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular Component Constructor Called Twice - Stack Overflow

programmeradmin1浏览0评论

I am new to Angular and am running into an issue with the constructor on a child component being called twice, and the second time it is called it is clearing the properties set the first time.

This is the parent component:

@Component({
    selector: 'parent-item',
    templateUrl: '...',
    providers: [ItemService]
})
export class ParentItemComponent {
    public parentItemId;
    public model: ParentItem;

    constructor(itemService: ItemService, elm: ElementRef) {
        this.parentItemId = elm.nativeElement.getAttribute('parentItemId'); 
        itemService.getParentItem(this.parentItemId).subscribe(data => this.model = data);
    }
}

And in the template the child component is referenced:

<child-items [parentItemId]="parentItemId">Loading...</<child-items>

This is the child component:

@Component({
    selector: 'child-items',
    templateUrl: '...',
    providers: [ItemService]
})
export class ChildItemsComponent {
    @Input() public parentItemId: number;
    public items: Observable<ChildItem[]>;

    constructor(private itemService: ItemService) {
        console.log("constructor");
    }

    ngOnInit() {
        if (this.parentItemId) {
            this.items = this.itemService.getChildItems(this.parentItemId);
        }  
        else {
            console.log("Parent Id not set!");
        }
    }
}

And finally the child component template:

<tr *ngFor="let item of items | async">
    <td>...</td>
</tr>

The child components constructor is being called twice, and the second time it is called the parentItemId is set to null and the items property is cleared. If I hardcode the parentId instead of using the input the data is being properly received and displayed in the template, but using the input value the template shows no results.

I have created a plunker which shows the same behavior here: /

I am new to Angular and am running into an issue with the constructor on a child component being called twice, and the second time it is called it is clearing the properties set the first time.

This is the parent component:

@Component({
    selector: 'parent-item',
    templateUrl: '...',
    providers: [ItemService]
})
export class ParentItemComponent {
    public parentItemId;
    public model: ParentItem;

    constructor(itemService: ItemService, elm: ElementRef) {
        this.parentItemId = elm.nativeElement.getAttribute('parentItemId'); 
        itemService.getParentItem(this.parentItemId).subscribe(data => this.model = data);
    }
}

And in the template the child component is referenced:

<child-items [parentItemId]="parentItemId">Loading...</<child-items>

This is the child component:

@Component({
    selector: 'child-items',
    templateUrl: '...',
    providers: [ItemService]
})
export class ChildItemsComponent {
    @Input() public parentItemId: number;
    public items: Observable<ChildItem[]>;

    constructor(private itemService: ItemService) {
        console.log("constructor");
    }

    ngOnInit() {
        if (this.parentItemId) {
            this.items = this.itemService.getChildItems(this.parentItemId);
        }  
        else {
            console.log("Parent Id not set!");
        }
    }
}

And finally the child component template:

<tr *ngFor="let item of items | async">
    <td>...</td>
</tr>

The child components constructor is being called twice, and the second time it is called the parentItemId is set to null and the items property is cleared. If I hardcode the parentId instead of using the input the data is being properly received and displayed in the template, but using the input value the template shows no results.

I have created a plunker which shows the same behavior here: http://embed.plnkr.co/xaJtfNgbWCUPap2RCJUA/

Share Improve this question edited Jul 19, 2018 at 12:08 Xameer 31.2k27 gold badges146 silver badges229 bronze badges asked Nov 18, 2016 at 17:59 rwdialrwdial 3331 gold badge4 silver badges9 bronze badges 1
  • 1 <child-items [parentItemId]="parentItemId">Loading...</<child-items> is the </< intentionally done that way? – smnbbrv Commented Nov 21, 2016 at 22:13
Add a comment  | 

2 Answers 2

Reset to default 10

Your problem is that in the app.module you bootstrap both parent and child component:

bootstrap:    [ ParentItemComponent, ChildItemsComponent ]

It has to be

  bootstrap:    [ ParentItemComponent]

child-items is not closed properly. Probably because of this error

This

<child-items [parentItemId]="parentItemId">Loading...</<child-items>

should be:

<child-items [parentItemId]="parentItemId">Loading...</child-items>
发布评论

评论列表(0)

  1. 暂无评论