1 pièce(s) jointe(s)
Angular 9 Custom input input ne s'affiche pas
app.module.ts
Code:
1 2 3 4 5 6
|
import { SharedModule } from './services/shared/shared.module';
imports: [
SharedModule,
], |
share.module.ts
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import { LienEditComponent } from '../../components/editable/lien-edit/lien-edit.component';
import { InputEditComponent } from '../../components/editable/input-edit/input-edit.component';
import { CustomInputComponent } from '../../components/custom-input.component';
@NgModule({
exports: [
CommonModule,
FormsModule,
ReactiveFormsModule,
CustomInputComponent
],
declarations: [
CustomInputComponent
],
})
export class SharedModule { } |
custom-input.component
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
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;
}
}
/*
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 = '';
}
*/ |
app.component.html
Code:
1 2 3 4 5 6
|
<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> |
list_component.html
Code:
1 2 3 4 5 6
|
<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" ngDefaultControl> Write in this wrapper control: </custom-input>
</form> |
affichage
Pièce jointe 576874
sur l'image, on voit bien que le custom-input du app.component ( au dessus dans l'ecran) fonctionne
et celui de la list ne s'affiche pas a l'exception des
Code:
1 2
| <p><span class="boldspan">Form data:</span>{{demoForm.value | json}}</p>
<p><span class="boldspan">Model data:</span> {{dataModel}}</p> |