我试图建立一个角指令,我想实现基于一些配置输入下面的东西值构建单指令来执行结构和属性行为?
- 在DOM基于输入值添加元素。 (就像ngIf)
- 添加一些造型,以元素,如果其呈现
- 像
disabled
添加一些属性属性元素
按我的一点知识和角度理解,我们可以通过实现第一个要求Structural Directive。在第二个&第三个需求我们需要创建Attribute Directive。这是我实现了两个指令
import { SomeService } from './some.service';
import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core';
@Directive({ selector: '[structuralBehaviour]' })
export class StructuralBehaviourDirective {
constructor(private templateRef: TemplateRef<any>,
private viewContainer: ViewContainerRef, private someService: SomeService) { }
@Input() set structuralBehaviour(config: any) {
// 1st Condition
// Conditional Stamentents using config and somService
// For the purpose to decide whether to add template in
// DOM or not
this.viewContainer.createEmbeddedView(this.templateRef);
}
}
下面是属性指令
import { SomeService } from './some.service';
import { Directive, ElementRef, Input, Renderer } from '@angular/core';
@Directive({ selector: '[attributeBehaviour]' })
export class AttributeBehaviourDirective {
constructor(private _renderer: Renderer, private _el: ElementRef,
private someService: SomeService) { }
@Input() set attributeBehaviour(config: any) {
// 2nd Condition
// Conditional Stamentents using config and someService
// For the purpose to set style visibility property to hidden
this._el.nativeElement.style.visibility = 'hidden';
// 3rd Condition
// Conditional Stamentents using config and someService
// For the purpose to add disabled attribute
this._renderer.setElementProperty(this._el.nativeElement, 'disabled, true);
}
}
目前,我正在使用上述指令喜欢遵循这对我来说
<button *structuralBehaviour="config" [attributeBehaviour]="config"
class="btn btn-primary">Add</button>
我在找工作正常在这里是回答问题,是否有可能将两个上面的指令合并在一起,并构建单个指令,以便我可以像这样使用它们
<button *singleBehaviour="config" class="btn btn-primary">Add</button>
阿贾克斯,你使用jQuery,或者可以将其包含在您的项目? jQuery可以快速解决您的问题。 – Chris
不,我没有使用JQuery,只是试图用AngularScript代码实现它。 – Ajax