2017-02-14 2 views
2

예 및 아니오 옵션 (확인 상자)이있는 대화 상자가 있습니다. 나는 사용자가 대화 상자에서 예 또는 아니오 버튼을 누를 때까지 기다리기를 원하지만, 대화 상자의 옵션을 클릭하기 전에 버튼을 클릭 할 때까지 콘솔 로그는 초기/빈 문자열을 인쇄합니다.MD 대화 상자 재질 각도 2 닫기 대기

HTML :

<button (click)="foo()"></button> 

구성 요소 :

selectedOption = ''; 

foo() { 
    this.openDialog(); 
    console.log('selectedOption: ' + this.selectedOption); // Prints initial Value 
    // hit the API if selectedOption is yes. 
} 

openDialog() { 
    dialogRef.afterClosed().subscribe(result => { 
    this.selectedOption = result; 
    }); 
} 

답변

1

그것은 당신이 코드를 작성하는 방식과 afterClosed는 관찰 가능한 비동기 적으로 반환 사실과 관련이있다. 당신이 빈 문자열 상단을 초기화하기 때문에이 지점 selectedOption에서 console.log(....); 를 호출 this.openDialog(); 에 통화가 여전히 비어 있습니다 후.

selectedOption = ''; 

foo() { 
    this.openDialog(); 
} 

openDialog() { 
    dialogRef.afterClosed().subscribe(result => { 
    this.selectedOption = result; 
    console.log('selectedOption: ' + this.selectedOption); // Prints 
    // hit the API if selectedOption is yes. 
    }); 
} 
:

는 그냥 CONSOLE.LOG과 구독 블록에 API를 논리를 이동