Angular 2 ng-bootstrap 模态

标签 angular typescript modal-dialog ng-bootstrap

我在应用程序中使用组件而不是模板打开模态然后我需要将对象模型传递给模态组件,问题是 typescript 给出了 modalRef.componentInstance 不存在作为属性的错误,我完全复制示例表单演示页面但再次给出相同的错误并且永远不会在模态内容类上填充@input变量,

这是错误 无法设置未定义的属性“模型”

 @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);
}
}

在模式中我声明这个变量来获取传入的值 @Input() 模型:公司; 但它始终为空

最佳答案

对于第一个 componentInstance 不存在的问题,我首先要确保您使用的是 1.0.0-alpha.9。当我使用 1.0.0-alpha.8 并升级修复了该问题时,我收到了同样的错误。

使用最新版本后,您的第二个问题似乎是您没有引用正确的 @Input() 变量。所以我会更改以下内容。

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

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

请注意,在更新的部分中,不是引用不存在的 name 变量,而是引用您的 @Input() 变量 model .

关于 Angular 2 ng-bootstrap 模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40195543/

相关文章:

java - 将 Angular 5 应用程序嵌入到 Jsp 项目的另一个应用程序中

javascript - Angular2 嵌套 formGroups - formArrays 和模板绑定(bind)

angular - <mat-spinner> 在我的 Angular 应用程序中完全静态

javascript - 阅读更多 阅读更少 陷入 Angular

iphone - PresentModalViewController 上的不同转换 :animated:

jquery - 适合移动设备的 Jquery 模态框吗?

angular - 即使指定了类型,在 Typescript 中创建新文件也会返回空类型

angular - 如何使用 *ngFor 迭代对象键?

reactjs - 汇总,TypeScript,Electron,React设置

delphi - 如何针对程序的某些(但不是全部)其他窗口创建对话框模式?