角度2(TypeScript)中的* ngIf的麻烦

我正在使用Angular 2 beta(TypeScript).我遇到了一个奇怪的问题.我试过Chrome,Firefox,Opera,都是一样的结果.

当我点击“切换”按钮,它可以成功显示/隐藏文本“Hello World!”.

当我使用套接字从另一个浏览器发送命令时,布尔值“show”会成功改变背景,但是文本不会显示/隐藏,看起来页面不会刷新.

import {Component,View} from 'angular2/core';
import {bootstrap} from 'angular2/bootstrap';
import {NgIf} from 'angular2/common';

@Component({
    selector: 'app'
})
@View({
    directives: [NgIf],template: `
      <button (click)="clicked()">Toggle</button>
      <div>
        <div *ngIf="show">
          <h2>Hello World!</h2>
        </div>
      </div>
    `
})
class App {
    show: boolean = true;

    constructor() {
        Socket.on {
            If (getMessage) {
                this.show = !this.show;
            }
        }
    }

    clicked() {
        this.show = !this.show;
    }
}

bootstrap(App);
我认为这是一个与Zone有关的问题.后者负责通知Angular它需要处理更新.

这个问题可以给你一些提示View is not updated on change in Angular2.

你可以尝试这样的事情:

export class App {
  constructor(ngzone:ngzone) {
    this.ngzone = ngzone;
    Socket.on {
      this.ngzone.run(() =>
        If (getMessage) {
            this.show = !this.show;
        }
      });
    }
  }

  }

这个问题也可能与你的问题有关:Angular2 child property change not firing update on bound property.

希望它能帮助你,蒂埃里

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...