2017-12-08 10 views
0

サービスレイヤからコンポーネントにjsonを渡してから、関数を順番に呼び出すのに苦労しています。http投稿の角5戻りオブジェクト

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です。どのように私はこれを修正することができます/私は間違って何ですか?

EDIT:コンポーネントのトップは、次のとおりです。

@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

addUserより長い500秒を取るのではなく、より利用者のサービスが完了したときに発することができる人工的なタイマーを作成することができるように思え。

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

プロミスなどでラップする必要はありません。

+0

私はそれを取得しません。私はあなたが示唆したように、まだそれは未定義です。 –