<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Quang Tang on Medium]]></title>
        <description><![CDATA[Stories by Quang Tang on Medium]]></description>
        <link>https://medium.com/@ttquang1063750?source=rss-4af7d5048c53------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*jJFUqKnjYQ5bR0G8CJxQFQ@2x.jpeg</url>
            <title>Stories by Quang Tang on Medium</title>
            <link>https://medium.com/@ttquang1063750?source=rss-4af7d5048c53------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 02:16:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ttquang1063750/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Angular: create extension markAllAsDisabled in AbstractControl]]></title>
            <link>https://ttquang1063750.medium.com/angular-create-extension-markallasdisabled-in-abstractcontrol-5b7019305be4?source=rss-4af7d5048c53------2</link>
            <guid isPermaLink="false">https://medium.com/p/5b7019305be4</guid>
            <category><![CDATA[angular-forms]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[angular-material]]></category>
            <dc:creator><![CDATA[Quang Tang]]></dc:creator>
            <pubDate>Tue, 27 Oct 2020 11:44:05 GMT</pubDate>
            <atom:updated>2020-10-27T11:44:05.846Z</atom:updated>
            <content:encoded><![CDATA[<p>You might know angular has a lot of features to make your form control deal with user behaviour such as: <strong>markAsTouched</strong>, <strong>markAsDirty</strong>, <strong>markAsPristine</strong>, etc. It will be cool if angular provides another way to disabled all form control instead of defined it one by one like below:</p><pre>this.form = this.formBuilder.group({<br>    name: new FormControl({ value: &quot;test name&quot;, disabled: true }),<br>    type: new FormControl({ value: &quot;test type&quot; })<br>});</pre><pre>// Or<br>this.form.get(&#39;type&#39;).disable();</pre><pre>// Or<br>enableForm(group: FormGroup, enable:boolean) {<br>   for (const i in group.controls) {<br>      if(enable) {<br>        group.controls[i].enable();<br>      } else {<br>        group.controls[i].disable();<br>      }<br>   }<br>}</pre><p>From the above, it’s just fine. But the code is not so clean.</p><p>With me, create an extension for AbstractControl more preferable.</p><p>Firstly, I create a file with the name is form.extensions.ts with this content</p><pre>import {AbstractControl} from &#39;@angular/forms&#39;;<br><br>declare module &#39;@angular/forms&#39; {<br>  interface AbstractControl {<br>    markAsDisabled(disable: boolean, onlySelf?: boolean): void;<br>    markAllAsDisabled(disable: boolean, onlySelf?: boolean): void;<br>  }<br>}<br><br>function markAsDisabled(disable: boolean, onlySelf?: boolean): void {<br>  if (disable) {<br>    this.disable();<br>  } else {<br>    this.enable();<br>  }<br><br>  if (this._parent &amp;&amp; !onlySelf) {<br>    this._parent.markAsDisabled(disable, onlySelf);<br>  }<br>}<br><br>function markAllAsDisabled(disable: boolean, onlySelf?: boolean): void {<br>  this.markAsDisabled(disable, onlySelf);<br><br>  <strong><em>Object</em></strong>.keys(this.controls).forEach(k =&gt; {<br>    (this.controls[k] as AbstractControl).markAsDisabled(disable, onlySelf);<br>  });<br>}<br><br>AbstractControl.prototype.markAsDisabled = markAsDisabled;<br>AbstractControl.prototype.markAllAsDisabled = markAllAsDisabled;</pre><p>then, we just basically import it at the top at main.ts</p><pre>import &#39;./app/form.extensions&#39;;</pre><pre>import {enableProdMode} from &#39;@angular/core&#39;;<br>import {<strong><em>platformBrowserDynamic</em></strong>} from &#39;@angular/platform-browser-dynamic&#39;;<br><br>import {AppModule} from &#39;./app/app.module&#39;;<br>import {<strong><em>environment</em></strong>} from &#39;./environments/environment&#39;;<br><br>if (<strong><em>environment</em></strong>.production) {<br>  enableProdMode();<br>}<br><br><strong><em>platformBrowserDynamic</em></strong>().bootstrapModule(AppModule)<br>  .catch(err =&gt; <strong><em>console</em></strong>.error(err));</pre><p>from now, your form has more power than before. You can mark your form as disabled for view only.</p><p>Let&#39;s create a form in app.component.ts</p><pre>import {<strong><em>Component</em></strong>} from &#39;@angular/core&#39;;<br>import {FormBuilder, FormControl, FormGroup} from &#39;@angular/forms&#39;;<br><br>@Component({<br>  selector: &#39;app-root&#39;,<br>  templateUrl: &#39;./app.component.html&#39;,<br>  styleUrls: [&#39;./app.component.scss&#39;]<br>})<br>export class AppComponent {<br>  form: FormGroup;<br><br>  constructor(private formBuilder: FormBuilder) {<br>    this.form = this.formBuilder.group({<br>      name: new FormControl(&#39;test name&#39;),<br>      description: new FormControl(&#39;test description&#39;)<br>    });<br>  }<br><br>  disabled() {<br>    this.form.markAllAsDisabled(true);<br>  }<br><br>  enabled() {<br>    this.form.markAllAsDisabled(false);<br>  }<br>}</pre><p>and app.component.html</p><pre>&lt;div [formGroup]=&quot;form&quot; class=&quot;example-container&quot;&gt;<br>  &lt;mat-form-field appearance=&quot;outline&quot;&gt;<br>    &lt;mat-label&gt;Name&lt;/mat-label&gt;<br>    &lt;input matInput formControlName=&quot;name&quot; /&gt;<br>  &lt;/mat-form-field&gt;<br>  &lt;br /&gt;<br>  &lt;mat-form-field appearance=&quot;outline&quot;&gt;<br>    &lt;mat-label&gt;Description&lt;/mat-label&gt;<br>    &lt;textarea matInput formControlName=&quot;description&quot;&gt;&lt;/textarea&gt;<br>  &lt;/mat-form-field&gt;<br>&lt;/div&gt;<br><br>&lt;div class=&quot;example-button-row&quot;&gt;<br>  &lt;button<br>    (click)=&quot;enabled()&quot;<br>    mat-raised-button<br>    color=&quot;primary&quot;<br>  &gt;<br>    Enable<br>  &lt;/button&gt;<br>  &lt;button<br>    (click)=&quot;disabled()&quot;<br>    mat-raised-button<br>    color=&quot;accent&quot;<br>  &gt;<br>    Disabled<br>  &lt;/button&gt;<br>&lt;/div&gt;</pre><p>And the result is</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*jfzv2tlRpv926i4sQ9mCPQ.gif" /></figure><p>Here is the <a href="https://stackblitz.com/edit/angular-mark-all-disabled?file=src/app/form-field-overview-example.html">demo</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5b7019305be4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular-material: Create a separate format for each date picker on the same page]]></title>
            <link>https://ttquang1063750.medium.com/angular-material-create-a-separate-format-for-each-date-picker-on-the-same-page-e9e35fda1b56?source=rss-4af7d5048c53------2</link>
            <guid isPermaLink="false">https://medium.com/p/e9e35fda1b56</guid>
            <category><![CDATA[angular-forms]]></category>
            <category><![CDATA[angular-material]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Quang Tang]]></dc:creator>
            <pubDate>Sat, 17 Oct 2020 17:48:44 GMT</pubDate>
            <atom:updated>2020-10-17T17:48:44.019Z</atom:updated>
            <content:encoded><![CDATA[<p>As I know in angular-material official, they showed you how to configure the format applies to all date-picker in the whole application or component level. But in the real case, we need to apply a separate format to each date-picker instead of only at the component.</p><p>So, I will show you how to customize it(or you can go to the <a href="https://stackblitz.com/edit/angular-material-create-a-separate-format-for-each-date-picker?file=src/app/app.component.html">demo</a> to see how it works)</p><p>Firstly, I will create a class CustomDateFormat at file ./custom-date-format.ts to process the parse and desiplay format as bellow:</p><pre>export interface DateParse { dateInput: string; }<br>export type DateDisplay = DateParse &amp; {<br>  monthYearLabel?: string,<br>  dateA11yLabel?: string,<br>  monthYearA11yLabel?: string,<br>};<br>export class CustomDateFormat {<br>  private _parse: DateParse = {<br>    dateInput: &#39;YYYY/MM/DD&#39;<br>  };<br>  private _display: DateDisplay = {<br>    dateInput: &#39;YYYY/MM/DD&#39;,<br>    monthYearLabel: &#39;MMM YYYY&#39;,<br>    dateA11yLabel: &#39;LL&#39;,<br>    monthYearA11yLabel: &#39;MMM YYYY&#39;<br>  };<br><br>  set parse(parse: DateParse) {<br>    this._parse = <strong><em>Object</em></strong>.assign({}, this._parse, parse);<br>  }<br><br>  get parse(): DateParse {<br>    return this._parse;<br>  }<br><br>  set display(display: DateDisplay) {<br>    this._display = <strong><em>Object</em></strong>.assign({}, this._display, display);<br>  }<br><br>  get display(): DateDisplay {<br>    return this._display;<br>  }<br><br>  updateDateFormat(parse: DateParse, display?: DateDisplay) {<br>    this.parse = parse;<br>    if (!display) {<br>      display = parse;<br>    }<br>    this.display = display;<br>  }<br>}</pre><p>As you can see the format above I have used @angular/material-moment-adapter. So, don’t forget to npm install @ngular/material-moment-adapter --save into your app.</p><p>Secondly, we create a directive to manipulate the <strong><em>MAT_DATE_FORMATS</em></strong></p><pre>import {<strong><em>Directive</em></strong>, <strong><em>Inject</em></strong>, <strong><em>Input</em></strong>, <strong><em>Optional</em></strong>} from &#39;@angular/core&#39;;<br>import {NgControl} from &#39;@angular/forms&#39;;<br>import {DateAdapter, <strong><em>MAT_DATE_FORMATS</em></strong>} from &#39;@angular/material/core&#39;;<br>import {CustomDateFormat, DateDisplay, DateParse} from &#39;./custom-date-format&#39;;<br>import {MomentDateAdapter} from &#39;@angular/material-moment-adapter&#39;;<br><br>@Directive({<br>  selector: &#39;[datePickerFormat]&#39;,<br>  providers: [<br>    {<br>      provide: DateAdapter,<br>      useClass: MomentDateAdapter<br>    },<br>    {<br>      provide: <strong><em>MAT_DATE_FORMATS</em></strong>,<br>      useClass: CustomDateFormat<br>    }<br>  ]<br>})<br>export class DatePickerFormatDirective {<br>  @Input() public configDateParse: DateParse;<br>  @Input() public configDateDisplay: DateDisplay;<br><br>  @Input(&#39;datePickerFormat&#39;)<br>  set datePickerFormat(format: string) {<br>    if (this.configDateParse) {<br>      this.matDateFormat.updateDateFormat(<br>        this.configDateParse,<br>        this.configDateDisplay<br>      );<br>    } else {<br>      this.matDateFormat.updateDateFormat({dateInput: format});<br>    }</pre><pre>    // We need this for the first time to tell component change new format<br>    const value = this.ngControl.value;<br>    this.ngControl.valueAccessor?.writeValue(value);<br>  }<br><br>  constructor(<br>    @Inject(<strong><em>MAT_DATE_FORMATS</em></strong>) public matDateFormat: CustomDateFormat,<br>    @Optional() private ngControl: NgControl<br>  ) {<br>  }<br>}</pre><p>Thirdly, inject DatePickerFormatDirective to theAppModule like this</p><pre>import { BrowserModule } from &quot;@angular/platform-browser&quot;;<br>import { <strong><em>NgModule </em></strong>} from &quot;@angular/core&quot;;<br>import { BrowserAnimationsModule } from &quot;@angular/platform-browser/animations&quot;;<br>import { FormsModule, ReactiveFormsModule } from &quot;@angular/forms&quot;;<br>import { MaterialModule } from &quot;./material.module&quot;;<br>import { MatDatepickerModule } from &quot;@angular/material/datepicker&quot;;<br>import { MatMomentDateModule } from &quot;@angular/material-moment-adapter&quot;;<br><br>import { AppComponent } from &quot;./app.component&quot;;<br>import { DatePickerFormatDirective } from &quot;./date-picker-format.directive&quot;;<br><br>@NgModule({<br>  declarations: [AppComponent, DatePickerFormatDirective],<br>  imports: [<br>    BrowserModule,<br>    BrowserAnimationsModule,<br>    MaterialModule,<br>    FormsModule,<br>    ReactiveFormsModule,<br>    MatDatepickerModule,<br>    MatMomentDateModule<br>  ],<br>  bootstrap: [AppComponent]<br>})<br>export class AppModule {}</pre><p>Finally, here are the use cases</p><p>At app.component.ts we define the form with data is new Date() at the FormControl</p><pre>import {<strong><em>Component</em></strong>, ViewEncapsulation} from &#39;@angular/core&#39;;<br>import {FormBuilder, FormControl, FormGroup} from &#39;@angular/forms&#39;;<br><br>@Component({<br>  selector: &#39;app-root&#39;,<br>  templateUrl: &#39;./app.component.html&#39;,<br>  styleUrls: [&#39;./app.component.scss&#39;],<br>  encapsulation: ViewEncapsulation.<em>None<br></em>})<br>export class AppComponent {<br>  form: FormGroup;<br><br>  constructor(private fb: FormBuilder) {<br>    this.form = this.fb.group({<br>      format1: new FormControl(new <strong><em>Date</em></strong>()),<br>      format2: new FormControl(new <strong><em>Date</em></strong>())<br>    });<br>  }<br><br>  onSubmit() {<br>    <strong><em>console</em></strong>.log(this.form.value);<br>  }<br>}</pre><p>And in template app.component.html</p><pre>&lt;div class=&quot;container&quot;&gt;<br>  &lt;div class=&quot;segment&quot;&gt;<br>    &lt;form [formGroup]=&quot;form&quot; (ngSubmit)=&quot;onSubmit()&quot;&gt;<br>      &lt;div class=&quot;block&quot;&gt;<br>        &lt;mat-form-field&gt;<br>          &lt;mat-label&gt;Date Picker (format: YYYY/MM/DD)&lt;/mat-label&gt;<br>          &lt;input<br>            matInput<br>            [matDatepicker]=&quot;<strong><em>datepicker</em></strong>&quot;<br>            datePickerFormat=&quot;YYYY/MM/DD&quot;<br>            formControlName=&quot;format1&quot;<br>          &gt;<br>          &lt;mat-datepicker-toggle matSuffix [for]=&quot;<strong><em>datepicker</em></strong>&quot;&gt;&lt;/mat-datepicker-toggle&gt;<br>          &lt;mat-datepicker #<strong><em>datepicker</em></strong>&gt;&lt;/mat-datepicker&gt;<br>        &lt;/mat-form-field&gt;<br>      &lt;/div&gt;<br><br>      &lt;div class=&quot;block&quot;&gt;<br>        &lt;mat-form-field&gt;<br>          &lt;mat-label&gt;Date Picker (format: MMMM Do YYYY)&lt;/mat-label&gt;<br>          &lt;input<br>            matInput<br>            [matDatepicker]=&quot;<strong><em>matDatepicker</em></strong>&quot;<br>            datePickerFormat=&quot;MMMM Do YYYY&quot;<br>            formControlName=&quot;format2&quot;<br>          &gt;<br>          &lt;mat-datepicker-toggle matSuffix [for]=&quot;<strong><em>matDatepicker</em></strong>&quot;&gt;&lt;/mat-datepicker-toggle&gt;<br>          &lt;mat-datepicker #<strong><em>matDatepicker</em></strong>&gt;&lt;/mat-datepicker&gt;<br>        &lt;/mat-form-field&gt;<br>      &lt;/div&gt;<br><br>      &lt;div class=&quot;block&quot;&gt;<br>        &lt;button mat-raised-button color=&quot;accent&quot; type=&quot;submit&quot;&gt;<br>          Submit<br>        &lt;/button&gt;<br>      &lt;/div&gt;<br>    &lt;/form&gt;<br>  &lt;/div&gt;<br>&lt;/div&gt;</pre><figure><img alt="Here is the result" src="https://cdn-images-1.medium.com/max/1024/1*DVFKS9LdwoLrdpXtdub5LA.png" /></figure><p>You can see the <a href="https://stackblitz.com/edit/angular-material-create-a-separate-format-for-each-date-picker?embed=1&amp;file=src/app/app.component.html">demo</a></p><p>That’s all.</p><p>Thank you for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e9e35fda1b56" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>