angular2:在自定义指令中设置@Input绑定属性的方法
我现在正从Angular1迁移到Angular2。在我的应用程序中,我在自定义指令部分遇到了一个令人困惑的问题。演示代码如下所示:angular2:在自定义指令中设置@Input绑定属性的方法,angular,set,directive,Angular,Set,Directive,我现在正从Angular1迁移到Angular2。在我的应用程序中,我在自定义指令部分遇到了一个令人困惑的问题。演示代码如下所示: import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core' @Directive({ selector: '[myUnless]' }) export class UnlessDirective { @Input('myUnless') s
import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core'
@Directive({
selector: '[myUnless]'
})
export class UnlessDirective {
@Input('myUnless')
set condition(newCondition:boolean) {
if {!newCondition} {
this.viewContainer.createEmbeddedView(this.templateRef);
} else {
this.viewContainer.clear();
}
}
constructor(
private templateRef: TemplateRef <any>,
private viewContainer: ViewContainerRef
) {}
}
<p *myUnless="boolValue"></p>
import{Directive,Input,TemplateRef,ViewContainerRef}来自'@angular/core'
@指示({
选择器:“[myuncern]”
})
出口类非关税指令{
@输入('my除非')
设置条件(新条件:布尔值){
如果{!newCondition}{
this.viewContainer.createEmbeddedView(this.templateRef);
}否则{
this.viewContainer.clear();
}
}
建造师(
私有templateRef:templateRef,
私有viewContainer:ViewContainerRef
) {}
}
这是一种结构指令,与NgIf相反我不明白的是
@Input
绑定部分。一般来说,格式如下:@Input('my除非')
条件
因此,条件
是可变的。这是可以理解的在上述情况下,根据我的搜索,
set
是条件的方法。不完全理解。这是怎么回事?绑定到属性时,set是访问器。这是一个typescript语法和用法。我会检查你的doc@jonrsharpe如果正确,则可以为每个属性设置一个set/get访问器。当您希望在设置新值时执行特定操作时,这非常有用。