创建具有输入属性字典的组件:
输入百分比。组成部分ts
import {
Component, ViewChild, ElementRef, AfterViewInit, Input
} from '@angular/core';
@Component({
selector: 'percent-input',
templateUrl: 'percent-input.component.html'
})
export class PercentInputComponent implements AfterViewInit {
@ViewChild('inputField') inputField: ElementRef;
@Input() props: { [key: string]: string };
constructor() { }
ngAfterViewInit() {
if (this.props) {
Object.keys(this.props).forEach( attr => {
this.inputField.nativeElement.setAttribute(attr, this.props[attr]);
});
}
}
}
输入百分比。组成部分html
<div class="input-group">
<input type="number" #inputField class="form-control" step="0.01">
<div class="input-group-append">
<span class="input-group-text">%</span>
</div>
</div>
像这样使用它
<percent-input [props]="{ style: 'background-color: yellow' }" ></percent-input>
Demo