javascript - Passing variable or setting a variable by URL in Angular 2 - Stack Overflow

I have an application written in Angular2. In one ponent, there's a chart, you can adjust it by se

I have an application written in Angular2. In one ponent, there's a chart, you can adjust it by setting few variables. I would like the user to have pletely set chart with these variables already set by the URL link.

For example: localhost:4200/app/chart?height=500;width=400;color=blue

Inside my ponent, there are variables called:

this.height: number;
this.width: number;
this.color: string;

And that is my question, how can I set them exactly from the URL link, on ponent load? (on init)

And my second question, how can I pass these variables into URL? For example, I have these variables:

this.height: number = 500;
this.width: number = 400;
this.color: string = blue;

How can I send them to the URL? To make it look like:

localhost:4200/app/chart?height=500;width=400;color=blue`

My routing file:

import { Routes, RouterModule } from '@angular/router';

import { ChartComponent } from './chart/chart/index';

const appRoutes: Routes = [
  { path: 'app', ponent: AppComponent,
    children: [
      { path: 'chart', ponent: ChartComponent },
      { path: '', ponent: DashboardComponent }
    ]},

  // otherwise redirect to home
  { path: '**', redirectTo: '' }
 ];

export const routing = RouterModule.forRoot(appRoutes);

I have an application written in Angular2. In one ponent, there's a chart, you can adjust it by setting few variables. I would like the user to have pletely set chart with these variables already set by the URL link.

For example: localhost:4200/app/chart?height=500;width=400;color=blue

Inside my ponent, there are variables called:

this.height: number;
this.width: number;
this.color: string;

And that is my question, how can I set them exactly from the URL link, on ponent load? (on init)

And my second question, how can I pass these variables into URL? For example, I have these variables:

this.height: number = 500;
this.width: number = 400;
this.color: string = blue;

How can I send them to the URL? To make it look like:

localhost:4200/app/chart?height=500;width=400;color=blue`

My routing file:

import { Routes, RouterModule } from '@angular/router';

import { ChartComponent } from './chart/chart/index';

const appRoutes: Routes = [
  { path: 'app', ponent: AppComponent,
    children: [
      { path: 'chart', ponent: ChartComponent },
      { path: '', ponent: DashboardComponent }
    ]},

  // otherwise redirect to home
  { path: '**', redirectTo: '' }
 ];

export const routing = RouterModule.forRoot(appRoutes);
Share Improve this question edited Mar 18, 2017 at 20:11 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Mar 15, 2017 at 21:09 J.DoeJ.Doe 511 silver badge3 bronze badges 2
  • Can you please paste your route configuration? I think your answer lies in the @angular/router namespace :) – A1rPun Commented Mar 15, 2017 at 21:18
  • 1 @A1rPun Done my friend – J.Doe Commented Mar 15, 2017 at 21:19
Add a ment  | 

1 Answer 1

Reset to default 4
constructor(private route: ActivatedRoute) {}

  ngOnInit() {
    this.route.params.subscribe(
      (params: any) => {
        this.height: number = params['height'];
        this.width: number = params['width'];
        this.color: string = params['color'];
      }
    );
  }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745566443a4633422.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信