【发布时间】:2018-03-31 22:20:06
【问题描述】:
我有一个简单的按钮和一个指令,我想访问按钮的样式,在指令中添加带有 onclick() 函数的 MarginLeft,它不起作用,但从构造函数设置 css 有效,如何在单击时使用它?请帮忙:
指令:
import { Directive, ElementRef } from '@angular/core';
@Directive({
selector: '[Bluecolored]'
})
export class BluecoloredDirective {
constructor(private element:ElementRef) {
console.log(element);
element.nativeElement.style.color="blue";
}
clicked(){
this.element.nativeElement.style.marginLeft=20;
console.log("marzi"+this.element.nativeElement.style.marginLeft);
}
}
这是模板:
<p Bluecolored>
new-fom works!
</p>
<h1 Bluecolored>Blue Colored</h1>
<button (click)="clicked()" Bluecolored>Click</button>
【问题讨论】:
标签: javascript css angular typescript angularjs-directive