2017-11-13 11 views
0

私はang 4アプリで@ ng-bootstrapを使用しています。ng-bootstrapモーダルTypeError約束

モーダルインスタンスを開くことができ、データをモーダルに渡すことができました。モーダルで閉じる 私はモーダルから結果を得ることができました。

私が直面している問題は、モーダルが使用されている親コンポーネント内のいくつかの既存の変数を結果に割り当てることです。 これはエラーの下に表示されています。以下は

ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'push' of undefined TypeError: Cannot read property 'push' of undefined

私はすべてのヘルプが理解されるであろうモーダル

import { Component, OnInit, Input } from '@angular/core'; 
import { AdminService } from "../admin.service"; 
import { NgbModal, ModalDismissReasons, NgbAlertConfig } from '@ng-bootstrap/ng-bootstrap'; 
import { NgAddModalComponent } from './add-modal.component'; 

@Component({ 
    selector: 'app-name-list', 
    templateUrl: './name-list.component.html', 
    styleUrls: ['./name-list.component.css'], 
    providers: [NgbAlertConfig] 
}) 
export class NameListComponent implements OnInit { 
    errorMessage: string; 
    name: string; 
    nameList: []; 

    constructor(private adminService: AdminService, private modalService: NgbModal) {} 

    ngOnInit() { 
     this.adminService.getNamelist() 
     .subscribe(res => { 
      this.nameList = res.name; //["john", "smith", "todd"] 
     }, 
     error => this.errorMessage = <any>error); 
    } 

    onAddClick() { 
     const modalRef = this.modalService.open(NgAddModalComponent, {size: 'lg'});//To open Modal 
     modalRef.componentInstance.name = this.name;//To send data to Modal 
     modalRef.result.then((result) => {//To subscribe data from Modal 
     this.nameList.push(result.name); //Getting error on this line 
    }); 
    } 
} 

を表示するように書かれているコードです!

答えて

1

ネームリストの宣言が間違っています。

あなたはこのようにそれを宣言する必要があります: nameList = [];

この好きではない:実際に私が名前リストによって査読されたモデルを持っている nameList: [];

+0

を。 nameList:NameModel []; – subrat71