2017-12-08 3 views
0

서비스 계층에서 구성 요소로 json을 전달한 다음 순차적으로 함수를 호출하는 데 어려움을 겪고 있습니다.http post에서 Angular 5 return 객체

users.service.ts 

addUser(user: User) { 
    return this.http.post('http://localhost:8080/user/create', user).map(res => res.json()); 
    } 

다음 구성 요소 계층 : 여기에 이미 성취 한 것입니다

async onSubmit(model) { 
await new Promise(resolve => { 
    setTimeout(() => { 
    this.submitted = true; 
    this.usersService.addUser(model).subscribe(data => this.savedModel = data); 
    resolve(); 
    }, 500); 
}); 
this.sendNotification(); 
} 

@Output() notifyParent: EventEmitter<any> = new EventEmitter(); 
sendNotification() { 
this.notifyParent.emit(JSON.stringify(this.savedModel)); 
} 

savedModelundefined입니다. 이 문제를 어떻게 해결할 수 있습니까?/내가 뭘 잘못하고 있니?

편집 : 구성 요소의 상단은 다음과 같습니다 addUser 긴 500 초 복용 할 수있다처럼

@Component({ 
selector: 'app-user-form', 
templateUrl: './user-form.component.html', 
styleUrls: ['./user-form.component.css'] 
}) 
export class UserFormComponent implements OnInit { 

groups: Group[]; 
model: User; 
savedModel; 
+0

구성 요소에'savedModel'을 (를) 선언 했습니까? – cyberpirate92

+0

부모 구성 요소의 모습 (자식 태그 및 이와 관련된 TS 코드) – Alex

답변

1

것 같다 아니라 사용자 서비스가 완료 될 때 발생할 수있는 인공 타이머를 만들어보다.

onSubmit(modal) { 
    this.usersService.addUser(model).subscribe(data => { 
    this.savedModel = data; 
    this.sendNotification(); // or just .emit here 
    }); 
} 

Promise 또는 기타 내용으로 포장하지 않아도됩니다.

+0

알 수 없습니다. 나는 당신이 제안한대로했고 여전히 정의되지 않았다. –