如何在自定义组件中使用ngModel?

网址:https://embed.plnkr.co/nqKUSPWb6w5QXr8a0wEu/?show=preview

import { Component, forwardRef } from '@angular/core';
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';

const noop = () => {
};

export const CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR: any = {
    provide: NG_VALUE_ACCESSOR,
    useExisting: forwardRef(() => CustomInputComponent),
    multi: true
};

@Component({
    selector: 'custom-input',
    template: `<div class="form-group">
                    <label><ng-content></ng-content>
                        <input [(ngModel)]="value"  
                                class="form-control" 
                                (blur)="onBlur()" >
                    </label>
                </div>`,
    providers: [CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR]
})
export class CustomInputComponent implements ControlValueAccessor {

    //The internal data model
    private innerValue: any = '';

    //Placeholders for the callbacks which are later provided
    //by the Control Value Accessor
    private onTouchedCallback: () => void = noop;
    private onChangeCallback: (_: any) => void = noop;

    //get accessor
    get value(): any {
        return this.innerValue;
    };

    //set accessor including call the onchange callback
    set value(v: any) {
        if (v !== this.innerValue) {
            this.innerValue = v;
            this.onChangeCallback(v);
        }
    }

    //Set touched on blur
    onBlur() {
        this.onTouchedCallback();
    }

    //From ControlValueAccessor interface
    writeValue(value: any) {
        if (value !== this.innerValue) {
            this.innerValue = value;
        }
    }

    //From ControlValueAccessor interface
    registerOnChange(fn: any) {
        this.onChangeCallback = fn;
    }

    //From ControlValueAccessor interface
    registerOnTouched(fn: any) {
        this.onTouchedCallback = fn;
    }

}

使用

//our root app component
import {Component} from '@angular/core'

@Component({
    selector: 'demo-app',
    template: `
        <p><span class="boldspan">Form data:</span>{{demoForm.value | json}}</p>
        <p><span class="boldspan">Model data:</span> {{dataModel}}</p>
        <form #demoForm="ngForm">
          <custom-input name="someValue" 
                          [(ngModel)]="dataModel">
              Write in this wrapper control:
          </custom-input>
        </form>`
})
export class AppComponent {
    dataModel: string = '';
}

原理:

  • 需要继承ControlValueAccessor 。
  • writeValue用来获取ngModel的值,onChangeCallback 用来将ngModel的值传出。每次改变value时都要触发这个函数。
  • 注意当ngmodel传入的值没有改变时,不会触发writeValue。
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,270评论 19 139
  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 14,764评论 0 38
  • Vue.js是一个很优秀的前端框架,熟练运用可以帮助您快速搭建各种应用。如何理解Vue.js教程中的“使用自定义事...
    daling菜鸟阅读 5,104评论 1 3
  • 今天晚上一起看了贝贝熊系列的《成绩单大麻烦》,小熊哥哥把时间都放在了体育上,所以得了一份很糟糕的成绩单,被愤怒的爸...
    铜豌豆xyn阅读 3,153评论 2 1
  • 《论语》由孔子弟子及再传弟子编写而成,至汉代成书。主要记录孔子及其弟子的言行,这里面有不少是曾参的学生所记载的。较...
    清心阁阅读 1,648评论 0 0

友情链接更多精彩内容