dev-resources.site
for different kinds of informations.
Auto binding Input() and Output() in Angular9+
Published at
3/14/2020
Categories
angular
binding
directives
io
Author
endykaufman
Author
11 person written this
endykaufman
open
Directives for auto binding Input() and Output() from host component to inner in Angular9+ application
Installation
npm i --save ngx-bind-io
Example
Without auto binding inputs and outputs
<component-name (start)="onStart()" [isLoading]="isLoading$ | async" [propA]="propA" [propB]="propB"> </component-name>
With auto binding inputs and outputs
<component-name [bindIO]> </component-name>
app.module.ts
import { NgxBindIOModule } from 'ngx-bind-io';
import { InnerComponent } from './inner.component';
import { HostComponent } from './host.component';
@NgModule({
...
imports: [
...
NgxBindIOModule.forRoot(), // in child modules import as "NgxBindIOModule"
...
]
declarations: [
...
InnerComponent,
HostComponent
...
],
...
})
export class AppModule { }
inner.component.ts
import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core';
@Component({
selector: 'inner',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<div *ngIf="isLoading">Loading... (5s)</div>
<button (click)="onStart()">Start</button> <br />
{{ propA }} <br />
{{ propB }}
`
})
export class InnerComponent {
@Input()
isLoading: boolean;
@Input()
propA = 'Prop A: not defined';
@Input()
propB = 'Prop B: not defined';
@Output()
start = new EventEmitter();
onStart() {
this.start.next(true);
}
}
base-host.component.ts
import { BehaviorSubject } from 'rxjs';
export class BaseHostComponent {
isLoading$ = new BehaviorSubject(false);
onStart() {
this.isLoading$.next(true);
setTimeout(() => this.isLoading$.next(false), 5000);
}
}
host.component.ts
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { BaseHostComponent } from './base-host.component';
@Component({
selector: 'host',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<inner [bindIO]></inner>
`
})
export class HostComponent extends BaseHostComponent {
propA = 'Prop A: defined';
propB = 'Prop B: defined';
}
Debug
For global debug all bindings
import { NgxBindIOModule } from 'ngx-bind-io';
@NgModule({
...
imports: [
...
NgxBindIOModule.forRoot({debug: true})
...
],
...
})
export class AppModule { }
For debug on one place
<comp-name [bindIO]="{debug:true}"></comp-name>
Rules for detect inputs and outputs
Custom rules for detect output method
my-ngx-bind-outputs.service.ts
import { Injectable } from '@angular/core';
import { IBindIO, NgxBindOutputsService } from 'ngx-bind-io';
@Injectable()
export class MyNgxBindOutputsService extends NgxBindOutputsService {
checkKeyNameToOutputBind(directive: Partial<INgxBindIODirective>, hostKey: string, innerKey: string) {
const outputs = directive.outputs;
const keyWithFirstUpperLetter =
innerKey.length > 0 ? innerKey.charAt(0).toUpperCase() + innerKey.substr(1) : innerKey;
return (
(hostKey === `on${keyWithFirstUpperLetter}` &&
outputs.hostKeys.indexOf(`on${keyWithFirstUpperLetter}Click`) === -1 &&
outputs.hostKeys.indexOf(`on${keyWithFirstUpperLetter}ButtonClick`) === -1) ||
(hostKey === `on${keyWithFirstUpperLetter}Click` &&
outputs.hostKeys.indexOf(`on${keyWithFirstUpperLetter}ButtonClick`) === -1) ||
hostKey === `on${keyWithFirstUpperLetter}ButtonClick`
);
}
}
app.module.ts
import { NgxBindOutputsService, NgxBindIOModule } from 'ngx-bind-io';
import { MyNgxBindOutputsService } from './shared/utils/my-ngx-bind-outputs.service';
import { InnerComponent } from './inner.component';
import { HostComponent } from './host.component';
@NgModule({
declarations: [AppComponent],
imports: [
...
NgxBindIOModule,
...
],
declarations: [
AppComponent,
InnerComponent,
HostComponent,
...
],
providers: [
...
{ provide: NgxBindOutputsService, useClass: MyNgxBindOutputsService },
...
],
bootstrap: [AppComponent]
})
export class AppModule { }
Links
Demo - Demo application with ngx-bind-io.
Stackblitz - Simply sample of usage on https://stackblitz.com
binding Article's
20 articles in total
Safely unwrap optional values in SwiftUI bindings
read article
Safely restructure your codebase with Dependency Graphs
read article
Data not updated?WPF
read article
What is Property binding and how to implement it in Angular?
read article
This is awkward in JavaScript
read article
How to Bind to a DataTemplate in UWP
read article
JavaScript functional binding(thisExplained)
read article
Six ways to use data binding with Vue.js
read article
Binding a service to a micro-service using odo
read article
Develop a TextField validator for SwiftUI (Episode 2)
read article
Settling the editors key bindings war
read article
Vue two way data-binding in custom checkbox
read article
React Simple Data Binding example
read article
Angular Learning Day 3: Displaying Data and Handling Events
read article
Auto binding Input() and Output() in Angular9+
currently reading
VUE v-model, two-way data binding and editing in multi nested components, dynamic components
read article
Angular Done Right: Part 1 — Binding and Directives
read article
Using PatchValue on FormArrays in Angular 7
read article
Binding in React
read article
React Tips: 2 - Binding an Event Handler
read article
Featured ones: