代码之家  ›  专栏  ›  技术社区  ›  John Montgomery

将标签连接到复选框组件

  •  1
  • John Montgomery  · 技术社区  · 8 年前

    我创建了一个自定义复选框组件,如下所示:

    checkbox.component.ts复选框

    import {Component, Input, OnInit, forwardRef} from "@angular/core"
    import {ControlValueAccessor, NG_VALUE_ACCESSOR} from "@angular/forms"
    
    @Component({
        selector: "checkbox",
        template: `
            <input type="checkbox" [checked]="checked" (change)="checkedChanged($event)" [id]="id">
            <label [for]="id"><span>{{checked ? "&#10003;" : "&nbsp;"}}</span></label>
        `,
        styles: [`
            input {
                opacity: 0;
                position: fixed;
            }
            label {
                line-height: 16px;
                height: 16px;
                width: 16px;
                border-radius: 5px;
                font-size: 16px;
                color: #000000;
                background-color: #ffffff;
                margin-bottom: 0;
    
                -webkit-touch-callout: none;
                -webkit-user-select: none;
                -khtml-user-select: none;
                -moz-user-select: none;
                -ms-user-select: none;
                user-select: none;
            }
        `],
        providers: [
            {
                provide: NG_VALUE_ACCESSOR,
                useExisting: forwardRef(() => CheckboxComponent),
                multi: true
            }
        ]
    })
    export class CheckboxComponent implements OnInit, ControlValueAccessor {
        static idCounter = 1
    
        @Input() id: string
    
        checked: boolean
        propagateChange = (_: any) => {}
        onTouchedCallback = () => {}
    
        ngOnInit () {
            // If an ID wasn't provided, generate a unique one
            if (!this.id) {
                this.id = "checkboxcomponent" + CheckboxComponent.idCounter++
            }
        }
    
        checkedChanged (event) {
            this.checked = event.target.checked
            this.propagateChange(event.target.checked)
        }
    
        // ControlValueAccessor requirements
        writeValue (value: any) {
            this.checked = value
        }
        registerOnChange (func: any) {
            this.propagateChange = func
        }
        registerOnTouched (func: any) {
            this.onTouchedCallback = func
        }
    }
    

    HTML示例

    <div class="col-4 text-right">
        <label for="foo">Foo:</label>
    </div>
    <div class="col-8">
        <checkbox [(ngModel)]="bar" id="foo"></checkbox>
    </div>
    

    复选框本身工作得很好,但标签没有。我想将id作为输入传入,以便将其连接到外部标签(标签在应用程序的不同部分的位置不同,因此我无法将其包含在组件中),检查页面显示它使用了正确的id,但单击标签不会切换复选框。我认为这是组件的某种范围问题,但我不确定如何处理。有没有一种方法可以让它工作而不必添加额外的 (click) 每次我用它的时候都是什么功能?

    1 回复  |  直到 8 年前
        1
  •  4
  •   k.s.    8 年前

    如果你改变

    @Input id: string;
    

    到

    @Input checkboxId: string;
    

    把它用在 <checkbox [(ngModel)]="bar" checkboxId="foo"></checkbox> 一切都会好起来的。

    https://stackblitz.com/edit/angular-qxjb4q

    推荐文章