Angular 2在url中传递id时显示?complntId = TS0000031

时间:2017-05-21 11:26:46

标签: angular angular2-routing angular2-services

有一个场景,如果用户输入一个号码并提交,则应导航到该ID并显示详细信息。

当我导航网址时,它显示的内容为

complaintstatus / TS0000031?complntId = TS0000031

详情视图代码:

export class ComplaintDetailComponent implements OnInit {
    id:number;
    private sub:any;
    complntId : any;
    constructor(private ComponentService:ComplaintService,private route:ActivatedRoute){

    }
    ngOnInit() {

   this.sub= this.route.params.subscribe((params:Params)=>{
        this.ComponentService.getCompliants(params['complntId'])
    });
    console.log(this.sub);

  }

用户输入id代码:

export class ComplaintStatusComponent {

    constructor(private router:Router){}
getComplaintDetails(complntId){
  this.router.navigate(['/complaintstatus', complntId]);
}  

}

还有一个问题,我如何将用户输入id发送到另一个组件。

和服务代码:

export class ComplaintService{
    private _url:string ="http://192.168.0.106:8000/app/complaint/complaintstatus"
    constructor(private _http:Http){}
    getCompliants(complntId){
        return this._http.get(this._url + '/' + complntId).map((response:Response)=>response.json());

    }
}

路由模块:

const routes: Routes = [
    {path:'complaintregistration',component:ComplaintRegistartionComponent},
    {path:'complaintstatus',component:ComplaintStatusComponent},
    {path:'complaintstatus/:complntId',component:ComplaintDetailComponent}
];

0 个答案:

没有答案