Skip to content

Instantly share code, notes, and snippets.

@hijiangtao
Created January 30, 2020 10:58
Show Gist options
  • Save hijiangtao/1be6feba90294863a403316b3b9dc31e to your computer and use it in GitHub Desktop.
Save hijiangtao/1be6feba90294863a403316b3b9dc31e to your computer and use it in GitHub Desktop.
Angular ControlValueAccessor 介绍与实战代码
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HelloPageComponent } from './pages/hello/hello.component';
const routes: Routes = [
{
path: 'hello',
pathMatch: 'full',
component: HelloPageComponent
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
<router-outlet></router-outlet>
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'angular-start-app';
}
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HelloPageComponent } from './pages/hello/hello.component';
import { Counter } from './components/counter/counter.component';
import { ReactiveFormsModule } from '@angular/forms';
@NgModule({
declarations: [
AppComponent,
HelloPageComponent,
Counter,
],
imports: [
BrowserModule,
AppRoutingModule,
ReactiveFormsModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
<div>
<p>当前值: {{ count }}</p>
<button (click)="increment()"> + </button>
<button (click)="decrement()"> - </button>
</div>
import { Component, Input, forwardRef } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
export const EXE_COUNTER_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => Counter),
multi: true
};
@Component({
selector: 'app-counter',
templateUrl: './counter.component.html',
styleUrls: ['./counter.component.css'],
providers: [EXE_COUNTER_VALUE_ACCESSOR]
})
export class Counter implements ControlValueAccessor {
_count: number = 0;
get count() {
return this._count;
}
set count(value: number) {
this._count = value;
this.propagateOnChange(this._count);
}
propagateOnChange: (value: any) => void = (_: any) => {};
propagateOnTouched: (value: any) => void = (_: any) => {};
ngOnInit() {}
writeValue(value: any) {
if (value) {
this.count = value;
}
}
registerOnChange(fn: any) {
this.propagateOnChange = fn;
}
registerOnTouched(fn: any) {
this.propagateOnTouched = fn;
}
increment() {
this.count++;
}
decrement() {
this.count--;
}
}
<ng-container>
<p>counter 控件测试</p>
<form [formGroup]="helloForm">
<app-counter formControlName="counter"></app-counter>
</form>
</ng-container>
import { Component, OnInit, Input } from '@angular/core';
import { FormGroup, FormBuilder } from '@angular/forms';
@Component({
selector: 'app-hello-page',
templateUrl: './hello.component.html',
styleUrls: ['./hello.component.css'],
})
export class HelloPageComponent implements OnInit {
helloForm: FormGroup;
constructor(private fb: FormBuilder) { }
ngOnInit() {
this.helloForm = this.fb.group({
counter: 5 // 设置初始值
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment