Skip to content

Instantly share code, notes, and snippets.

View armanozak's full-sized avatar

Levent Arman Özak armanozak

View GitHub Profile
@armanozak
armanozak / sample.component.ts
Last active May 9, 2019 08:24
A Sample Component with a Value Accessible via FormControlName, FormControlDirective or NgModel Directives #blog
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { Component, forwardRef, Input } from '@angular/core';
@Component({
providers: [{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => SampleComponent),
multi: true,
}],
@armanozak
armanozak / abstract-ng-model.component.shell.ts
Last active May 9, 2019 08:24
The Shell of an Abstract Component with Accessible Value #blog
import { ControlValueAccessor } from '@angular/forms';
import { Component } from '@angular/core';
@Component({ template: '' })
export class AbstractNgModelComponent<T = any> implements ControlValueAccessor {
/*
This is where boilerplate code for ControlValueAccessor
and any other reusable property or method will be placed
*/
@armanozak
armanozak / abstract-ng-model.component.ts
Last active June 4, 2019 08:05
An Abstract Component with Accessible Value and More #blog
import { ControlValueAccessor } from '@angular/forms';
import { ChangeDetectorRef, Component, Injector, Input, Type } from '@angular/core';
import { uuid } from '../utils/generators';
@Component({ template: '' })
export class AbstractNgModelComponent<T = any> implements ControlValueAccessor {
@Input()
cid: string = uuid();
@Input()
@armanozak
armanozak / abstract-input.component.ts
Last active May 9, 2019 08:25
An Abstract Input Component Extending Abstract NgModel Component #blog
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { AbstractNgModelComponent } from './ng-model.component';
@Component({ template: '' })
export class AbstractInputComponent extends AbstractNgModelComponent<string> {
@Input()
readonly: boolean = false;
@Input()
required: boolean = false;
@armanozak
armanozak / input-text.component.ts
Last active May 9, 2019 08:25
Two Input Components Extending Abstract Input Component #blog
import { ChangeDetectionStrategy, Component, forwardRef, Input } from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { AbstractInputComponent } from '../abstracts/input.component';
@Component({
selector: 'app-input-text',
template: `
<input [id]="cid"
[type]="type"
@armanozak
armanozak / app.component.ts
Last active May 9, 2019 08:30
Angular Context - One-way Data-binding (#1) #blog
@Component({
selector: 'my-app',
template: `
<context-provider
provide="progress progressStriped progressType"
[contextMap]="{progressType: 'type'}"
>
<one-way></one-way>
</context-provider>
`,
@armanozak
armanozak / one-way.component.ts
Last active May 9, 2019 08:30
Angular Context - One-way Data-binding (#2) #blog
@Component({
selector: 'one-way',
template: `
<context-consumer consume="progress"></context-consumer>
<progressbar
contextConsumer
[contextMap]="{progress: 'value', progressStriped: 'striped'}"
>{{ progress ? progress + '%' : '' }}</progressbar>
`,
@armanozak
armanozak / one-way.component.ts
Last active May 9, 2019 08:30
Angular Context - One-way Data-binding (#3) #blog
@Component({
selector: 'one-way',
template: `
<ng-template contextDisposer let-context>
<progressbar
[type]="context.type"
[value]="context.progress"
[striped]="context.progressStriped"
>{{ progress ? progress + '%' : '' }}</progressbar>
</ng-template>
@armanozak
armanozak / app.component.ts
Last active May 9, 2019 08:30
Angular Context - One-way Data-binding (#4) #blog
@Component({
selector: 'my-app',
template: `
<context-provider
provide="progress progressStriped progressType"
[contextMap]="{progressType: 'type'}"
>
<one-way></one-way>
</context-provider>
`,
@armanozak
armanozak / app.component.ts
Last active May 9, 2019 08:30
Angular Context - Two-way Data-binding (#1) #blog
@Component({
selector: 'my-app',
template: `
<context-provider provide="rate pre onRating">
<two-way></two-way>
</context-provider>
{{ rate }}
`,
})