Subscribing to ActivatedRoute Params in Angular 2 Does Not Update View Templates

I am not sure why change detection will not work here. I tried several things including setTimeout. I am using RC5. Basically, I want to change the content based on the parameter in my url. Should be pretty simple?

thanks.ts

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

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

thanks.html

<card>
  <div *ngIf="coming" class="card-content">
    <span class="card-title">We can't wait to see you!</span>
  </div>
  <div *ngIf="!coming" class="card-content">
    <span class="card-title">We will certainly miss you!</span>
  </div>
</card>
+4
source share
1 answer

Params can only be strings because they come from a URL

If you change it to

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

he should work.

+3
source

Source: https://habr.com/ru/post/1653406/


All Articles