Created
January 30, 2020 10:58
-
-
Save hijiangtao/1be6feba90294863a403316b3b9dc31e to your computer and use it in GitHub Desktop.
Angular ControlValueAccessor 介绍与实战代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<router-outlet></router-outlet> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component } from '@angular/core'; | |
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html', | |
styleUrls: ['./app.component.css'] | |
}) | |
export class AppComponent { | |
title = 'angular-start-app'; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div> | |
<p>当前值: {{ count }}</p> | |
<button (click)="increment()"> + </button> | |
<button (click)="decrement()"> - </button> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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--; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ng-container> | |
<p>counter 控件测试</p> | |
<form [formGroup]="helloForm"> | |
<app-counter formControlName="counter"></app-counter> | |
</form> | |
</ng-container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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