微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

角度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.

希望它能帮助你,蒂埃里

原文地址:https://www.jb51.cc/angularjs/142661.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐