in Education by
I need to wrap a mat-slide-toggle component on my own, I wrote: mytoggle.component.ts import { Component, OnInit, Input, forwardRef, ViewChild, ElementRef } from '@angular/core'; import {MatSlideToggle, MatSlideToggleChange} from '@angular/material/slide-toggle'; import { NG_VALUE_ACCESSOR } from '@angular/forms'; @Component({ selector: 'ng7-common-ng7-slide', templateUrl: 'ng7-slide.component.html', styles: [], providers: [ { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => Ng7SlideComponent), multi: true } ] }) export class Ng7SlideComponent extends MatSlideToggle { } And mytoggle.component.html: {{label}} and in my app I'm using like this: app.component.html <!-- THIS WORKS <mat-slide-toggle formControlName="slideToggle">Enable Wifi --> app.component.ts import { Component } from '@angular/core'; import { FormGroup, FormControl, FormBuilder } from '@angular/forms'; import { MatSlideToggleChange } from '@angular/material/slide-toggle'; @Component({ selector: 'home-root', templateUrl: './app.component.html', styleUrls: ['./app.component.scss'] }) export class AppComponent { formGroup: FormGroup; constructor(formBuilder: FormBuilder) { this.formGroup = formBuilder.group({ slideToggle: false }); } onFormSubmit(formValue: any) { alert(JSON.stringify(formValue, null, 2)); } } So the formValue on onFormSubmit method always alerts "slideToggle":false no matter is it is checked or not, when I use mat-slide-toggle it alerts true or false according with the toggle state correctly. Are there anything else to do? I just need to extend the component and all event. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
After some research I got something that works well.. I imported an abstract class that implements the basic value acessors methods: https://stackoverflow.com/a/45480791/2161180 import { ControlValueAccessor } from '@angular/forms'; export abstract class AbstractValueAccessor implements ControlValueAccessor { innerValue: any = ''; get value(): any { return this.innerValue; } set value(v: any) { if (v !== this.innerValue) { this.innerValue = v; this.onChange(v); } } writeValue(value: any) { this.innerValue = value; this.onChange(value); } onChange = (_) => {}; onTouched = () => {}; registerOnChange(fn: (_: any) => void): void { this.onChange = fn; } registerOnTouched(fn: () => void): void { this.onTouched = fn; } } Then I created my component extending it: import { NG_VALUE_ACCESSOR } from '@angular/forms'; import { Component, Input, forwardRef } from '@angular/core'; import { AbstractValueAccessor } from '../abstract.component'; @Component({ selector: 'my-switch', templateUrl: './my-switch.component.html', styleUrls: ['./my-switch.component.css'], providers: [ { provide: NG_VALUE_ACCESSOR, multi: true, useExisting: forwardRef(() => MySwitchComponent) } ] }) export class MySwitchComponent extends AbstractValueAccessor { @Input() label: string; @Input() checked: boolean; @Input() disabled: boolean; } html: {{label}} module: import { FormsModule } from '@angular/forms'; import { MatSlideToggleModule } from '@angular/material'; import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { MySwitchComponent } from './my-switch.component'; @NgModule({ declarations: [MySwitchComponent], imports: [ CommonModule, MatSlideToggleModule, FormsModule ], exports: [ MySwitchComponent ] }) export class MySwitchModule { } And to use it: Value: {{inputSwitch}} or Value: {{inputSwitchNgModel}}

Related questions

0 votes
    I'm trying to create an Angular reusable button component. Right now, it is used like so: [label] ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    How does one share data between components in Angular?...
asked Jun 30, 2021 in Technology by JackTerrance
0 votes
    What are the Components, Modules and Services in Angular?...
asked Jun 29, 2021 in Technology by JackTerrance
0 votes
    What is the need of Angular 8 components?...
asked Jun 28, 2021 in Technology by JackTerrance
0 votes
    There is one issue with the angular material with IE-11 and IE-edge browsers, When the body content ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to split a string with angularJS (4 answers) Closed 5 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to split a string with angularJS (4 answers) Closed 5 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    I have used angular-cli with systemJS and am now comfortable with its build process,test cases & component ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to split a string with angularJS (4 answers) Closed 5 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    I'm not sure why the change detection wouldn't work here. I've tried a few things, including a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    I'm not sure why the change detection wouldn't work here. I've tried a few things, including a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 16, 2022 in Education by JackTerrance
0 votes
    I have these routes /items/:id /items /items/:id/edit ... etc I want to put the component, to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    I have a mat-table with a component app-order-statuses on every row. the component calls the statuses ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    Following Regular Expression does work in HTML like following example: But if I want to put that pattern in ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I have a split screen design. I'd like to access the folder ID from the parent route only when ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 30, 2022 in Education by JackTerrance
...