javascript – 在Angular 2中订阅ActivatedRoute Params不会更新视图模板

前端之家收集整理的这篇文章主要介绍了javascript – 在Angular 2中订阅ActivatedRoute Params不会更新视图模板前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我不确定为什么变化检测在这里不起作用.我尝试过一些东西,包括一个setTimeout.我正在使用RC5.基本上我想根据我的URL中的参数更改内容.应该很简单吧?

thanks.ts

  1. import { Component } from '@angular/core';
  2. import { Card } from '../../components/card/card';
  3. import { ActivatedRoute } from '@angular/router';
  4. @Component({
  5. selector: 'sd-thanks',styleUrls: ['thanks.css'],directives: [Card],templateUrl: 'thanks.html'
  6. })
  7. export class Thanks {
  8. params: any;
  9. coming: any;
  10. constructor(public route: ActivatedRoute) {
  11. this.params = this.route.params.subscribe(
  12. params => {
  13. this.coming = params['coming'];
  14. console.log(this.coming); // this consoles the correct true/false value
  15. }
  16. );
  17. }
  18. }

thanks.html

最佳答案
Params只能是字符串,因为它们来自URL

如果你改成它

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

它应该工作.

猜你在找的JavaScript相关文章