[英]Angular 2 show popup and redirect
我需要显示弹出消息以显示一些消息,然后重定向到另一个组件。但是问题是它不显示弹出窗口,而是重定向到另一个组件。我尝试了以下代码将其重定向到另一个组件。因为我是新手,所以是angular 2请帮助我。
注意:popwindow显示如果我不重定向到另一个组件
import { Component,OnInit, ViewEncapsulation,ViewContainerRef } from '@angular/core';
import { NgForm } from '@angular/forms';
import { HttpService } from 'app/http.service';
import { Router } from '@angular/router';
import { Overlay } from 'angular2-modal';
import { Modal } from 'angular2-modal/plugins/bootstrap';
@Component({
selector: 'app-registration',
templateUrl: './registration.component.html',
styleUrls: ['./registration.component.css'],
encapsulation: ViewEncapsulation.None
})
export class RegistrationComponent implements OnInit {
constructor(private httpService : HttpService,private router: Router,overlay: Overlay, vcRef: ViewContainerRef, public modal: Modal) {
overlay.defaultViewContainer = vcRef;
}
ngOnInit() {
}
onsubmit(form: NgForm){
this.httpService.sendData(form.value).subscribe(data =>{
if(data.data == 1 ){this.modal.alert().title('Message').body('Successfully registered').open();
this.router.navigate(['/login']);}
},error => {
if(error)
{
this.modal.alert().title('Message').body('Server is not reachable').open();
}
});
}
}
这是因为您不必在指令之间等待。
您可以在this.router.navigate(['/login']);
之前添加this.router.navigate(['/login']);
计时器指令可能是2秒。
onsubmit(form: NgForm){
this.httpService.sendData(form.value).subscribe(data =>{
if(data.data == 1 ){this.modal.alert().title('Message').body('Successfully registered').open();
setTimeout(() => {
this.router.navigate(['/login']);
}, 2000);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.