Angular 输入更改时需要刷新指令
我作出了以下指示:Angular 输入更改时需要刷新指令,angular,typescript,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 { pri
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>
一个按钮!
两种方式
_isDisabled: boolean;
@Input('myDisabled')
set isDisable(value: boolean) {
this._isDisabled = value;
this.disable();
}
或
双向
_isDisabled: boolean;
@Input('myDisabled')
set isDisable(value: boolean) {
this._isDisabled = value;
this.disable();
}
或
尝试了第二种方法,工作完美,只需导入一次更改。谢谢也尝试了第一种方法,效果也不错,但我需要删除setterGreat的“:void”,感谢您的反馈(我自己没有主动使用TS)。尝试了第二种方法,效果很好,只需要导入一次更改。谢谢也尝试了第一种方法,效果也不错,但我需要删除setterGreat的“:void”,感谢您的反馈(我自己没有主动使用TS)。