小编Ale*_*ore的帖子

具有clipboardData属性的Angular2组件

我有一个Angular2组件,其中包含从剪贴板粘贴数据的方法:

inputPaste(event){
  let clipboardData = event.clipboardData;
  ...
Run Code Online (Sandbox Code Playgroud)

}

这种方式不适用于IE10 +,但是IE有一个带有属性clipboardData的窗口对象,但是typescript compilator会抛出一个错误:

inputPaste(event){
  let clipboardData = event.clipboardData 
            || window.clipboardData; //error 'clipboardData' does not exist on type Windows
  ...
Run Code Online (Sandbox Code Playgroud)

}

我找到了一个解决方案,我们必须使用angular2-clipboard指令,但我不想使用它.

我如何'windows.clipboardData'在打字稿中使用?

typescript angular

10
推荐指数
1
解决办法
3972
查看次数

使用 ChangeDetectionStrategy.OnPush 在 setTimeout 中不会触发更改检测

我有两个组件,我在其中使用 ChangeDetectionStrategy.OnPush。父组件:

    @Component({
     changeStaregy: ChangeDetectionStrategy.OnPush,
     template:`
        <button (click)="onClick()">clear</button>
        <div>
            <test [model]="model"></test>
        </div>`
    })
    export class AppComponent {
        model: TestModel;
        
        constructor(){
          this.model = { id: 1, text: 'bla bla bla'}
        }
    
        onClick() {
          this.model = new TestModel();
        }
    }
Run Code Online (Sandbox Code Playgroud)

和一个只显示数据的子组件:

    @Component({
       changeStrategy: ChangeDetectionStrategy.OnPush,
       selector: 'test',
       template: `
        <div>
              <div> {{model.id}} </div>
               <div> {{model.text}} </div>
        </div>`
    })
    
    export class TestComponent {
       @Input() model: TestModel;
       
    }
Run Code Online (Sandbox Code Playgroud)

当我单击“清除”按钮时,它会调用 onClick() 函数,该函数将一个空实体分配给“模型”。这会触发更改检测,因为输入已更改(OnPush 策略)。但是,如果我使用异步调用包装分配,则更改检测不起作用,因此 UI 不会更新:

    onClick() {
      setTimeout(() => {
        this.model = new TestModel();
      }, 2000); …
Run Code Online (Sandbox Code Playgroud)

angular

5
推荐指数
2
解决办法
4844
查看次数

标签 统计

angular ×2

typescript ×1