javascript - Subscribing to ActivatedRoute Params in Angular 2 does not update view templates - Stack Overflow

I'm not sure why the change detection wouldn't work here. I've tried a few things, inclu

I'm not sure why the change detection wouldn't work here. I've tried a few things, including a setTimeout. I'm using RC5. Basically I want to change the content based off of a parameter in my URL. Should be pretty simple right?

thanks.ts

import { Component } from '@angular/core';
import { Card } from '../../ponents/card/card';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'sd-thanks',
  styleUrls: ['thanks.css'],
  directives: [Card],
  templateUrl: 'thanks.html'
})
export class Thanks {
  params: any;
  ing: any;
  constructor(public route: ActivatedRoute) {
    this.params = this.route.params.subscribe(
      params => {
        thising = params['ing'];
        console.log(thising); // this consoles the correct true/false value
      }
    );
  }
}

thanks.html

<card>
  <div *ngIf="ing" class="card-content">
    <span class="card-title">We can't wait to see you!</span>
  </div>
  <div *ngIf="!ing" class="card-content">
    <span class="card-title">We will certainly miss you!</span>
  </div>
</card>

I'm not sure why the change detection wouldn't work here. I've tried a few things, including a setTimeout. I'm using RC5. Basically I want to change the content based off of a parameter in my URL. Should be pretty simple right?

thanks.ts

import { Component } from '@angular/core';
import { Card } from '../../ponents/card/card';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'sd-thanks',
  styleUrls: ['thanks.css'],
  directives: [Card],
  templateUrl: 'thanks.html'
})
export class Thanks {
  params: any;
  ing: any;
  constructor(public route: ActivatedRoute) {
    this.params = this.route.params.subscribe(
      params => {
        this.ing = params['ing'];
        console.log(this.ing); // this consoles the correct true/false value
      }
    );
  }
}

thanks.html

<card>
  <div *ngIf="ing" class="card-content">
    <span class="card-title">We can't wait to see you!</span>
  </div>
  <div *ngIf="!ing" class="card-content">
    <span class="card-title">We will certainly miss you!</span>
  </div>
</card>
Share Improve this question asked Sep 2, 2016 at 7:13 jaruesinkjaruesink 1,2032 gold badges14 silver badges24 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

Params can only be strings because they e from the URL

If you change it to

this.ing = params['ing'] && params['ing'].toLowerCase() === 'true';

it should work.

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信