Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 根据url动态更改内容-4_Angular_Url_Dynamic_Angular Routing - Fatal编程技术网

Angular 根据url动态更改内容-4

Angular 根据url动态更改内容-4,angular,url,dynamic,angular-routing,Angular,Url,Dynamic,Angular Routing,我有一个这样的url 在页面中,我有表单字段。表单字段之一是主题代码。现在,我希望主题代码在url中动态填充数字 用户可以更改url中的数字,这将更改页面中的内容 如果需要其他信息,请告诉我。谢谢 使用ActivatedRoute: import { Component, OnInit, OnDestroy } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; @Component({ sele

我有一个这样的url

在页面中,我有表单字段。表单字段之一是主题代码。现在,我希望主题代码在url中动态填充数字

用户可以更改url中的数字,这将更改页面中的内容


如果需要其他信息,请告诉我。谢谢

使用
ActivatedRoute

import { Component, OnInit, OnDestroy } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'subject',
  template: `
    <input formControlName="subject_code" [(ngModel)]="code" ngplaceholder="Subject Code" class="form-control>
  `,
})
export class SubjectComponent implements OnInit, OnDestroy {
  code: number;
  private sub: any;

  constructor(private route: ActivatedRoute) {
    this.code = 0;
  }

  ngOnInit() {
    this.sub = this.route.params.subscribe(params => {
       this.code = +params['code'];

       // In a real app: dispatch action to load the details here.
    });
  }

  ngOnDestroy() {
    this.sub.unsubscribe();
  }
}
从'@angular/core'导入{Component,OnInit,OnDestroy};
从'@angular/router'导入{ActivatedRoute};
@组成部分({
选择器:'主题',
模板:`

为什么要投否决票?如果有人解释他们的否决票,而不是简单地给出解释,那会很有帮助
 {
    path: 'subject/:code',
    component: SubjectComponent,
    loadChildren: './subject/subject.module#SubjectModule'
  },
import { Component, OnInit, OnDestroy } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'subject',
  template: `
    <input formControlName="subject_code" [(ngModel)]="code" ngplaceholder="Subject Code" class="form-control>
  `,
})
export class SubjectComponent implements OnInit, OnDestroy {
  code: number;
  private sub: any;

  constructor(private route: ActivatedRoute) {
    this.code = 0;
  }

  ngOnInit() {
    this.sub = this.route.params.subscribe(params => {
       this.code = +params['code'];

       // In a real app: dispatch action to load the details here.
    });
  }

  ngOnDestroy() {
    this.sub.unsubscribe();
  }
}