使用angular2单击提交按钮时,从一个页面导航到另一个页面

时间:2016-08-11 07:12:45

标签: javascript angular

当我点击提交按钮时,我试图在angularjs2中从一个页面导航到另一个页面。我尝试使用导航栏进行导航,但是当我点击按钮时我试图实现它时它无法正常工作?

这是我的代码

app.component.ts

import { Component } from 'angular2/core';
import { DetailsComponent } from './details.component';
import { Router,RouteConfig  } from 'angular2/router';
@Component({
  selector: 'my-app',
  template: `<div>
  <input  type="text" [(ngModel)]="myName" (keyup.enter)="myName='' placeholder="enter ur Name" >
  <input type="submit" class="btn" value="Continue" (click)="clickName(Name); Name=''" > 
        </div>`,
  directives:[DetailsComponent]
})

@RouteConfig([
  { path: '/details',  component: DetailsComponent,  name: 'Details' }
  ])
export class AppComponent {
   constructor(private router:Router){

 }
  clickName(name){
  console.log('in the clickName' + Name);
    this.router.navigate(['Details']);
  }}

details.component.ts

import {Component} from 'angular2/core';

@Component({
  selector: 'details',
  template: `<h1>In the second page of details form`

})

export class DetailsComponent{

}

这些是我的两个组件第一个组件是Appcomponent正在显示输入的模板,该模板具有名称以及何时提交它。我必须导航到另一个包含一些文本()的页面。我只需要导航它。请解决我的问题。我正在尝试过去2天

提前感谢。

2 个答案:

答案 0 :(得分:1)

按如下方式更改提交功能:

clickName(name){
    console.log('in the clickName' + Name);
    this.router.navigate(['details/']);    // pass route in the navigate function instead of Component Name
  }}

答案 1 :(得分:0)

使用路径路径而不是名称:

this.router.navigate([ '/细节']);