使用 Switchery 设置样式的自定义复选框输入组件
Custom checkbox input component styled with Switchery
我正在尝试创建一个带有 Switchery 样式的自定义复选框组件,它可以像任何其他 <input type="checkbox" ... />
组件一样在表单中使用。
我现在的代码负责样式设置:
import {Component,ViewChild,AfterViewInit,Input} from 'angular2/core';
import switchery from 'switchery';
@Component({
selector: 'switchery-checkbox',
template: `<input #checkbox type="checkbox" class="js-switch"/>`,
})
export class SwitcheryComponent implements AfterViewInit {
@Input() options: Switchery.Options = {};
@ViewChild('checkbox') checkbox: any;
ngAfterViewInit() {
new switchery(this.checkbox.nativeElement,
this.options);
}
}
我必须添加什么才能像下面的代码那样在模板中使用它?理想情况下,它应该实现 <input type="checkbox" />
.
的所有功能
<switchery-checkbox
[(ngModel)]="model.onOrOff"
ngControl="onOrOff"
[disabled]="disabledCondition"
... >
</switchery-checkbox>
事实上,您需要制作您的组件 "ngModel-compliant" 但实现自定义值访问器。
方法如下:
@Component({
selector: 'switchery-checkbox',
template: `
<input #checkbox type="checkbox" (change)="onChange($event.target.checked)" class="js-switch"/>
`,
(...)
})
export class SwitcheryComponent implements AfterViewInit, ControlValueAccessor {
@Input() options: Switchery.Options = {};
@Input() disabled:boolean = false;
@ViewChild('checkbox') checkbox: any;
value: boolean = false;
onChange = (_) => {};
onTouched = () => {};
writeValue(value: any): void {
this.value = value;
this.setValue(this.value);
}
registerOnChange(fn: (_: any) => void): void { this.onChange = fn; }
registerOnTouched(fn: () => void): void { this.onTouched = fn; }
ngAfterViewInit() {
this.switcher = new switchery(this.checkbox.nativeElement,
this.options);
this.setValue(this.value);
this.setDisabled(this.disabled);
}
ngOnChanges(changes: {[propName: string]: SimpleChange}) {
if (changes && changes.disabled) {
this.setDisabled(changes.disabled.currentValue);
}
}
setValue(value) {
if (this.switcher) {
var element = this.switcher.element;
element.checked = value
}
}
setDisabled(value) {
if (this.switcher) {
if (value) {
this.switcher.disable();
} else {
this.switcher.enable();
}
}
}
}
最后,您需要将值访问器注册到组件的 providers
中:
const CUSTOM_VALUE_ACCESSOR = new Provider(
NG_VALUE_ACCESSOR, {useExisting: forwardRef(() => SwitcheryComponent), multi: true});
@Component({
selector: 'switchery-checkbox',
template: `
<input #checkbox type="checkbox" (change)="onChange($event.target.checked)" class="js-switch"/>
`,
providers: [ CUSTOM_VALUE_ACCESSOR ]
})
export class SwitcheryComponent implements AfterViewInit, ControlValueAccessor {
(...)
}
这样你就可以这样使用你的指令了:
<switchery-checkbox [disabled]="disabled"
[(ngModel)]="value" ngControl="cb"
#cb="ngForm"></switchery-checkbox>
看到这个 plunkr:https://plnkr.co/edit/z1gAC5U0pgMSq0wicGHC?p=preview。
有关详细信息,请参阅本文(第 "NgModel-compatible component" 部分):
我正在尝试创建一个带有 Switchery 样式的自定义复选框组件,它可以像任何其他 <input type="checkbox" ... />
组件一样在表单中使用。
我现在的代码负责样式设置:
import {Component,ViewChild,AfterViewInit,Input} from 'angular2/core';
import switchery from 'switchery';
@Component({
selector: 'switchery-checkbox',
template: `<input #checkbox type="checkbox" class="js-switch"/>`,
})
export class SwitcheryComponent implements AfterViewInit {
@Input() options: Switchery.Options = {};
@ViewChild('checkbox') checkbox: any;
ngAfterViewInit() {
new switchery(this.checkbox.nativeElement,
this.options);
}
}
我必须添加什么才能像下面的代码那样在模板中使用它?理想情况下,它应该实现 <input type="checkbox" />
.
<switchery-checkbox
[(ngModel)]="model.onOrOff"
ngControl="onOrOff"
[disabled]="disabledCondition"
... >
</switchery-checkbox>
事实上,您需要制作您的组件 "ngModel-compliant" 但实现自定义值访问器。
方法如下:
@Component({
selector: 'switchery-checkbox',
template: `
<input #checkbox type="checkbox" (change)="onChange($event.target.checked)" class="js-switch"/>
`,
(...)
})
export class SwitcheryComponent implements AfterViewInit, ControlValueAccessor {
@Input() options: Switchery.Options = {};
@Input() disabled:boolean = false;
@ViewChild('checkbox') checkbox: any;
value: boolean = false;
onChange = (_) => {};
onTouched = () => {};
writeValue(value: any): void {
this.value = value;
this.setValue(this.value);
}
registerOnChange(fn: (_: any) => void): void { this.onChange = fn; }
registerOnTouched(fn: () => void): void { this.onTouched = fn; }
ngAfterViewInit() {
this.switcher = new switchery(this.checkbox.nativeElement,
this.options);
this.setValue(this.value);
this.setDisabled(this.disabled);
}
ngOnChanges(changes: {[propName: string]: SimpleChange}) {
if (changes && changes.disabled) {
this.setDisabled(changes.disabled.currentValue);
}
}
setValue(value) {
if (this.switcher) {
var element = this.switcher.element;
element.checked = value
}
}
setDisabled(value) {
if (this.switcher) {
if (value) {
this.switcher.disable();
} else {
this.switcher.enable();
}
}
}
}
最后,您需要将值访问器注册到组件的 providers
中:
const CUSTOM_VALUE_ACCESSOR = new Provider(
NG_VALUE_ACCESSOR, {useExisting: forwardRef(() => SwitcheryComponent), multi: true});
@Component({
selector: 'switchery-checkbox',
template: `
<input #checkbox type="checkbox" (change)="onChange($event.target.checked)" class="js-switch"/>
`,
providers: [ CUSTOM_VALUE_ACCESSOR ]
})
export class SwitcheryComponent implements AfterViewInit, ControlValueAccessor {
(...)
}
这样你就可以这样使用你的指令了:
<switchery-checkbox [disabled]="disabled"
[(ngModel)]="value" ngControl="cb"
#cb="ngForm"></switchery-checkbox>
看到这个 plunkr:https://plnkr.co/edit/z1gAC5U0pgMSq0wicGHC?p=preview。
有关详细信息,请参阅本文(第 "NgModel-compatible component" 部分):