输入更改时需要刷新指令

时间:2016-08-30 15:41:48

标签: angular typescript

我做了以下指令:

import { Directive, ElementRef, HostListener, Input, OnInit } from '@angular/core';
import { Observable, Subscription } from 'rxjs/Rx';

@Directive({
    selector: '[myDisabled]'
})

export class DisableDirective implements OnInit {
    private el: HTMLElement;

    @Input('myDisabled') isDisable: boolean;

    constructor(el: ElementRef) { this.el = el.nativeElement;}

    ngOnInit() {
        this.disable();
    }

    private disable() {
        this.isDisable ? this.el.style.opacity = '0.65' : this.el.style.opacity = '1';
    }
}

该指令设置按钮的不透明度取决于输入:isDesable。当输入改变时,需要刷新此设置,但是,我不知道如何操作。

我像这样使用这个指令:

<button class="btn" [myDisabled]="!sharedDetails.isEnabled">A button !</button>

1 个答案:

答案 0 :(得分:0)

两种方式

// called every time after inputs are updated
// check the `changes` parameter for details if you have more than one input
ngOnChanges(changes) {
  this.disable();    
}

{{1}}