angular 2 ng-bootstrap modal

那年仲夏 提交于 2021-01-27 12:12:36

问题


i open modal in app with component instead of template then i need to pass the object model to modal component, the problem is that the typescript gives error of modalRef.componentInstance not exists as property, i exactly copy the sample form demo page but again give the same error and never get the @input variable populated on modal content class,

this is error Cannot set property 'model' of undefined

 @Component({
selector: 'company-list',
templateUrl: './app/components/company/company-list.component.html'
})
export class CompanyListComponent implements {
private modalRes: Company;

constructor(private modalService: NgbModal) {
}

open(company: Company) {
    const modalRef = this.modalService.open(CompanyAddComponent);
    modalRef.componentInstance.name = 'some name';  //** this line gives error

    modalRef.result.then((result) => {           
    }, (reason) => {

    });     
}

createCompany(model: Company) {
    this.companyService.createCompany(model);
}
}

and inside the modal i declare this varable to get the passed in value @Input() model: Company; but it is always null


回答1:


For your first issue where componentInstance does not exist, I would first make sure that you are working with 1.0.0-alpha.9. I received this same error when I was working with 1.0.0-alpha.8 and upgrading fixed that issue.

Once you are working with the most recent version, your second issue seems to be that you are not referencing the correct @Input() variable. So I would change the following.

// Original
...
modalRef.componentInstance.name = 'some name'; 
...

// Updated
...
modalRef.componentInstance.model = new Company();
...

Notice in the updated section, instead of referencing the name variable that does not exist it is referencing your @Input() variable model.



来源:https://stackoverflow.com/questions/40195543/angular-2-ng-bootstrap-modal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!