[英]router.navigate doesn't work when called from button element in angular2
This is my one of the components: 这是我的组件之一:
@Component({
selector: 'login-view',
templateUrl: 'app/login/login.component.html',
directives: [MATERIAL_DIRECTIVES, FORM_DIRECTIVES]
})
export class LoginComponent implements OnInit{
loginForm: ControlGroup;
constructor(
private _formBuilder: FormBuilder, private _router: Router) {
}
ngOnInit() {
// some stuffs
}
submitLoginForm(){
// to be implemented
}
goToRegister(){
this._router.navigate(['/Register']);
}
goToResetPassword(){
this._router.navigate(['/ResetPassword']);
}
}
This is the template for the above component: 这是上述组件的模板:
<form [ngFormModel]="loginForm" (ngSubmit)="submitLoginForm()">
<input md-input ngControl="mobileNumber" type="number" id="mobileNumber">
<input md-input type="password" ngControl="password" id="password"/>
<button md-raised-button type="submit" class="md-raised md-primary">Login</button>
<div>
// These Work
<a md-button class="md-primary" (click)="goToResetPassword()">Forgot password?</a>
<a md-button class="md-primary" (click)="goToRegister()">New here? Register</a>
//These don't work, reloads the app
<button md-button class="md-primary" (click)="goToResetPassword()">Forgot password?</button>
<button md-button class="md-primary" (click)="goToRegister()">New here? Register</button>
</div>
</form>
If am calling goToResetPassword()
or goToRegister()
from (click)
of <a>
it works as it is supposed to. 如果要从
<a>
(click)
调用goToResetPassword()
或goToRegister()
,它将按预期工作。
But, it doesn't work If I change the html element to <button>
, it just reloads the whole app without throwing any error in the console. 但是,如果我将html元素更改为
<button>
,它将无法重新加载整个应用程序而不会在控制台中引发任何错误。
Try this may work for you : - 试试这个可能对您有用:-
<a type="button" class="btn "
[routerLink]="['/Foo']">{{labels.CancelBtnText}}</a>
This might also work 这可能也可以
(click)="goToResetPassword();false"
to prevent the default submit behavior of the button. 以防止按钮的默认提交行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.