<?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[Learn Ng - Medium]]></title>
        <description><![CDATA[Angular TL;DR articles for a quick productivity boost. - Medium]]></description>
        <link>https://medium.com/learn-ng?source=rss----23699d95b94c---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Learn Ng - Medium</title>
            <link>https://medium.com/learn-ng?source=rss----23699d95b94c---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 18 May 2026 11:32:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/learn-ng" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Reactive Form]]></title>
            <link>https://medium.com/learn-ng/reactive-form-c738fe4142b9?source=rss----23699d95b94c---4</link>
            <guid isPermaLink="false">https://medium.com/p/c738fe4142b9</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[learn-angular]]></category>
            <dc:creator><![CDATA[Ye Min Ko]]></dc:creator>
            <pubDate>Thu, 07 Oct 2021 06:15:54 GMT</pubDate>
            <atom:updated>2021-10-07T06:15:54.531Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ayAI1mag2T_Gg6IW" /><figcaption>Photo by <a href="https://unsplash.com/@neonbrand?utm_source=medium&amp;utm_medium=referral">NeONBRAND</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Form control လုပ်နိုင်ဖို့ ဒုတိယနည်းလမ်းဖြစ်တဲ့ Reactive Form ကိုလေ့လာရပါမယ်။ Reactive Form ဆိုတာ TypeScript ဘက်ကနေ Form ကိုအပြည့်အဝ ထိန်းချုပ်ပြီး Template နဲ့ sync လုပ်ပေးတဲ့နည်း ဖြစ်ပါတယ်။</p><p>အသုံးပြုဖို့အတွက် ReactiveFormsModule ကို AppModule မှာ import လုပ်ပေးထားဖို့လိုအပ်ပါတယ်။</p><pre><strong>import { ReactiveFormsModule } from &#39;@angular/forms&#39;;</strong></pre><pre>@NgModule({<br>imports: [ <strong>ReactiveFormsModule</strong> ]<br>...<br>})<br>export class AppModule {}</pre><p>ReactiveFormComponent ကို generate လုပ်ပြီးအောက်ပါ code တွေကိုရေးပါ။</p><pre><strong><em>HTML</em></strong><br>&lt;form&gt;<br>  &lt;div class=&quot;form-group&quot;&gt;<br>    &lt;label&gt;Username&lt;/label&gt;<br>    &lt;input type=&quot;text&quot; class=&quot;form-control&quot; /&gt;<br>  &lt;/div&gt;<br>  &lt;div class=&quot;form-group&quot;&gt;<br>    &lt;label&gt;Mail&lt;/label&gt;<br>    &lt;input type=&quot;email&quot; class=&quot;form-control&quot; /&gt;<br>  &lt;/div&gt;<br>  &lt;div class=&quot;form-group&quot;&gt;<br>    &lt;label&gt;Secret Questions&lt;/label&gt;<br>    &lt;select class=&quot;form-control&quot;&gt;<br>      &lt;option value=&quot;pet&quot;&gt;Your first pet?&lt;/option&gt;<br>      &lt;option value=&quot;teacher&quot;&gt;Your first teacher?&lt;/option&gt;<br>    &lt;/select&gt;<br>  &lt;/div&gt;&lt;div class=&quot;radio&quot; *ngFor=&quot;let gender of genders&quot;&gt;<br>    &lt;label&gt;<br>      &lt;input type=&quot;radio&quot; [value]=&quot;gender&quot; name=&quot;gender&quot; /&gt;<br>      {{ gender }}<br>    &lt;/label&gt;<br>  &lt;/div&gt;<br>  &lt;br /&gt;<br>  &lt;button class=&quot;btn btn-primary&quot; type=&quot;submit&quot;&gt;Submit&lt;/button&gt;<br>&lt;/form&gt;</pre><pre><strong><em>TypeScript<br></em></strong>genders = [&#39;male&#39;, &#39;female&#39;];</pre><p>အခုလိုရလဒ် မျိုးရလာမှာပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/963/1*0iwS_Y6e4IiLxudpWXwjEA.png" /><figcaption>Simple form</figcaption></figure><p>Reactive Form သည် TypeScript ကနေ control လုပ်တာဖြစ်လို့ အောက်ပါအတိုင်း FormGroup ကိုအရင် initialize လုပ်ထားဖို့ လိုအပ်ပါတယ်။</p><pre>signupForm!: <strong>FormGroup</strong>;</pre><pre>ngOnInit() {<br>    this.signupForm = new <strong>FormGroup</strong>({<br>      username: new <strong>FormControl</strong>(null),<br>      email: new <strong>FormControl</strong>(null),<br>      secret: new <strong>FormControl</strong>(&#39;pet&#39;),<br>      gender: new <strong>FormControl</strong>(&#39;male&#39;),<br>    });<br> }</pre><p>FormGroup ဆိုတာ form ကို control လုပ်မဲ့ FormControl တွေစုစည်းထားတဲ့ obj ဖြစ်ပါတယ်။ ဒါကြောင့် အဲ့ဒီထဲမှာ အသုံးပြုချင်တဲ့ FormControl တွေကို ကြိုတင် သတ်မှတ်ထားတာ တွေ့နိုင်ပါတယ်။</p><p>FormControl ရဲ့ first parameter မှာ input field အတွက် initialize value ကိုသတ်မှတ်နိုင်ပါတယ်။</p><p>ပြီးရင်တော့ သတ်မှတ်လိုက်တဲ့ FormGroup ကို template နဲ့ ချိတ်ဆက်ပေးဖို့ အောက်ပါအတိုင်းရေးပါ။</p><pre>&lt;form <strong>[formGroup]</strong>=&quot;signupForm&quot;&gt;<br>...<br>&lt;/form&gt;</pre><p>Angular ကို အလိုလျှောက် JS Obj မဆောက်ခိုင်းတော့ပဲ ကိုယ်တိုင်ဆောက်ထားတဲ့ FormGroup ကို သုံးပေးဖို့ ပြောလိုက်တာ ဖြစ်ပါတယ်။</p><p>ဆက်လက်ပြီး FormControl တွေကို input field တွေနဲ့ ချိတ်ဆက်ပေးပါ။</p><pre>&lt;input type=&quot;text&quot; class=&quot;form-control&quot; <strong>formControlName</strong>=&quot;username&quot; /&gt;</pre><pre>&lt;input type=&quot;email&quot; class=&quot;form-control&quot; <strong>formControlName</strong>=&quot;email&quot; /&gt;</pre><pre>&lt;select class=&quot;form-control&quot; <strong>formControlName</strong>=&quot;secret&quot;&gt;...&lt;/select&gt;</pre><pre>&lt;input type=&quot;radio&quot; [value]=&quot;gender&quot; name=&quot;gender&quot;<br> <strong>formControlName</strong>=&quot;gender&quot;<br>/&gt;</pre><p>formControlName ဆိုတဲ့ directive နဲ့ သတ်မှတ်ခဲ့တဲ့ FormControl တွေရဲ့ နံမည်ကိုထည့်ပေးလိုက်တာ တွေ့ရမှာပါ။</p><p>ဒါဆိုရင် form နဲ့ FormGroup တို့ချိတ်ဆက်တာပြီးပါပြီ။ Submit လုပ်ရင် data ရယူဖို့အတွက် အောက်ပါအတိုင်းရေးပါ။</p><pre><strong><em>HTML</em></strong><br>&lt;form [formGroup]=&quot;signupForm&quot; <strong>(ngSubmit)=&quot;onSubmit()&quot;</strong>&gt;...&lt;/form&gt;</pre><pre><strong><em>TypeScript</em></strong><br>onSubmit() {<br>    console.log(this.<strong>signupForm</strong>);<br>    console.log(&#39;Username: &#39; + this.signupForm.value.username);<br>    console.log(&#39;Email: &#39; + this.signupForm.value.email);<br>    console.log(&#39;Secret Question: &#39; + this.signupForm.value.secret);<br>    console.log(&#39;Gender: &#39; + this.signupForm.value.gender);<br>}</pre><p>Template-Driven ကဲ့သို့ ngSubmit နဲ့ရယူလိုက်ပါတယ်။ ဒါပေမဲ့ local reference တွေတော့မလိုတော့ပါ။ Submit နှိပ်လိုက်ရင် အခုလို တွေ့ရမှာပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/876/1*1hzWMm1C27ZhVJUHiQH72g.png" /><figcaption>Form’s data</figcaption></figure><p>ဒီနည်းနဲ့ အလွယ်တကူ data ရယူနိုင်မှာပဲ ဖြစ်ပါတယ်။</p><h3>Form Validation</h3><p>Validation သတ်မှတ်ဖို့ အခုလိုရေးပါ။</p><pre>ngOnInit(): void {<br>    this.signupForm = new FormGroup({<br>      username: new FormControl(null, <strong>Validators.required</strong>),<br>      email: new FormControl(null, <strong>[Validators.required,   <br>      Validators.email]</strong>),<br>      ...<br>    });<br>}</pre><p>FormControl ရဲ့ second parameter မှာ Validators တွေ သတ်မှတ်နိုင်ပါတယ်။ တစ်ခုထပ်ပို သတ်မှတ်ချင်ရင် [ ] နဲ့ ထည့်နိုင်ပါတယ်။</p><blockquote>တခြား validators တွေကို <a href="https://angular.io/api/forms/Validators"><strong>ဒီမှာ</strong></a> လေ့လာနိုင်ပါတယ်။</blockquote><p>ဆက်လက်ပြီး invalid data ဆိုရင် input field ကို error ဖြစ်နေကြောင်း မြင်သာအောင် ယခုလိုရေးပါ။</p><pre>input.ng-touched.ng-invalid {<br>  border: 1px solid red;<br>}</pre><p>Error message ပြပေးနိုင်ဖို့ အခုလို ဆက်ရေးပါ။</p><pre>&lt;span<br>      class=&quot;form-text&quot;<br>      *ngIf=&quot;<br>        signupForm.<strong>get(&#39;username&#39;)</strong>!.invalid &amp;&amp;<br>        signupForm.<strong>get(&#39;username&#39;)</strong>!.touched<br>      &quot;<br>      &gt;Please enter a username!&lt;/span<br>    &gt;</pre><pre>&lt;span<br>      class=&quot;form-text&quot;<br>      *ngIf=&quot;<br>        signupForm.<strong>get(&#39;email&#39;)</strong>!.invalid &amp;&amp;   <br>        signupForm.<strong>get(&#39;email&#39;)</strong>!.touched<br>      &quot;<br>      &gt;Please enter a valid email!&lt;/span<br>    &gt;</pre><p>signupForm ကနေ get() နဲ FormControl ကိုဆွဲထုတ်ပြီး property တွေထုတ်ကာ စစ်ထားတာ ဖြစ်ပါတယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/965/1*H5N0uH352D9ezjfpxYMkTQ.png" /><figcaption>Form with validation</figcaption></figure><p>ဒီနည်းနဲ့အခုလို အလွယ်တကူ validation ပြုလုပ်နိုင်မှာ ဖြစ်ပါတယ်။</p><p>Submit လုပ်ပြီ form ကို reset လုပ်ချင်ရင် ယခုလို လုပ်နိုင်ပါတယ်။</p><pre>onSubmit() {<br>this.signupForm.<strong>reset</strong>({<br>      secret: &#39;pet&#39;,<br>      gender: &#39;male&#39;,<br>    });<br>}</pre><p>ဒါဆိုရင်တော့ Reactive Form ကိုသုံးပြီး form တွေကို control လုပ်နိုင်ပြီပဲ ဖြစ်ပါတယ်။</p><p><strong>Previous: </strong><a href="https://medium.com/learn-ng/template-driven-form-58cfeebe03fe"><strong>Template-Driven Form</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c738fe4142b9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/learn-ng/reactive-form-c738fe4142b9">Reactive Form</a> was originally published in <a href="https://medium.com/learn-ng">Learn Ng</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Template-Driven Form]]></title>
            <link>https://medium.com/learn-ng/template-driven-form-58cfeebe03fe?source=rss----23699d95b94c---4</link>
            <guid isPermaLink="false">https://medium.com/p/58cfeebe03fe</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[learn-angular]]></category>
            <dc:creator><![CDATA[Ye Min Ko]]></dc:creator>
            <pubDate>Wed, 06 Oct 2021 15:27:59 GMT</pubDate>
            <atom:updated>2021-10-07T06:16:22.781Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Zo4khjPHygJqRPTo" /><figcaption>Photo by <a href="https://unsplash.com/@wesleyphotography?utm_source=medium&amp;utm_medium=referral">Wesley Tingey</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Form တွေ valid ဖြစ်မဖြစ် လိုချင်တဲ့ data ဟုတ်မဟုတ် စစ်ဖို့ Angular မှာ နည်းလမ်းအမျိုးမျိုးရှိပါတယ်။ အဲ့ဒီထဲက Template-Driven နဲ့ form control လုပ်နည်းကို လေ့လာကြပါမယ်။</p><p>Template-Driven Form ဆိုတာ template ထဲမှာတိုက်ရိုက် control လုပ်တဲ့ နည်းလမ်းဖြစ်ပါတယ်။ အသုံးပြုနိုင်ဖို့အတွက် FormsModule ကို AppModule ထဲမှာ အခုလို import လုပ်ပေးထားဖို့ လိုအပ်ပါတယ်။</p><pre>@NgModule({<br>imports: [<strong>FormsModule</strong>, ...],<br>...<br>})<br>export class AppModule {}</pre><p>အရင်ဆုံး FormDemo component ကို generate လုပ်ပြီးအောက်ပါ code တွေရေးပါ။</p><pre><strong><em>HTML</em></strong><br>&lt;form&gt;<br>  &lt;div class=&quot;form-group&quot;&gt;<br>    &lt;label&gt;Username&lt;/label&gt;<br>    &lt;input type=&quot;text&quot; class=&quot;form-control&quot; /&gt;<br>  &lt;/div&gt;<br>  &lt;div class=&quot;form-group&quot;&gt;<br>    &lt;label&gt;Mail&lt;/label&gt;<br>    &lt;input type=&quot;email&quot; class=&quot;form-control&quot; /&gt;<br>  &lt;/div&gt;<br>  &lt;div class=&quot;form-group&quot;&gt;<br>    &lt;label&gt;Secret Questions&lt;/label&gt;<br>    &lt;select class=&quot;form-control&quot;&gt;<br>      &lt;option value=&quot;pet&quot;&gt;Your first pet?&lt;/option&gt;<br>      &lt;option value=&quot;teacher&quot;&gt;Your first teacher?&lt;/option&gt;<br>    &lt;/select&gt;<br>  &lt;/div&gt;</pre><pre>&lt;div class=&quot;radio&quot; *ngFor=&quot;let gender of genders&quot;&gt;<br>    &lt;label&gt;<br>      &lt;input type=&quot;radio&quot; [value]=&quot;gender&quot; name=&quot;gender&quot; /&gt;<br>      {{ gender }}<br>    &lt;/label&gt;<br>  &lt;/div&gt;<br>  &lt;br /&gt;<br>  &lt;button class=&quot;btn btn-primary&quot; type=&quot;submit&quot;&gt;Submit&lt;/button&gt;<br>&lt;/form&gt;</pre><pre><strong><em>TypeScript<br></em></strong>genders = [&#39;male&#39;, &#39;female&#39;];</pre><p>run လိုက်ရင် အခုလို ရလဒ်တွေ့ရမှာ ဖြစ်ပါတယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/855/1*8nOYHX6shSU8h1OCcXm1eQ.png" /><figcaption>Sample Form</figcaption></figure><p>Angular က &lt;form&gt; tag ကိုတွေ့ရင် control လုပ်နိုင်ဖို့ နောက်ကွယ်မှာ အလိုလျှောက် JS obj တစ်ခုဆောက်ပေးပါတယ်။ ဒါပေမဲ့ form ထဲက ဘယ် input field တွေကို control လုပ်ချင်လဲ ဆိုတာကတော့ ကိုယ်တိုင်သတ်မှတ်ပေးမှ ရပါမယ်။</p><p>ဒီလိုသတ်မှတ်ဖို့အတွက် control လုပ်စေချင်တဲ့ input field တွေမှာ ngModel နဲ့ name ကို ထည့်ပေးရပါမယ်။</p><pre>&lt;form&gt;<br>  &lt;div class=&quot;form-group&quot;&gt;<br>    &lt;label&gt;Username&lt;/label&gt;<br>    &lt;input type=&quot;text&quot; class=&quot;form-control&quot; <strong>ngModel name=&quot;username&quot;</strong> /&gt;<br>  &lt;/div&gt;<br>  &lt;div class=&quot;form-group&quot;&gt;<br>    &lt;label&gt;Mail&lt;/label&gt;<br>    &lt;input type=&quot;email&quot; class=&quot;form-control&quot; <strong>ngModel name=&quot;email&quot;</strong> /&gt;<br>  &lt;/div&gt;<br>  &lt;div class=&quot;form-group&quot;&gt;<br>    &lt;label&gt;Secret Questions&lt;/label&gt;<br>    &lt;select class=&quot;form-control&quot; <strong>ngModel name=&quot;secret</strong>&quot;&gt;<br>      &lt;option value=&quot;pet&quot;&gt;Your first pet?&lt;/option&gt;<br>      &lt;option value=&quot;teacher&quot;&gt;Your first teacher?&lt;/option&gt;<br>    &lt;/select&gt;<br>  &lt;/div&gt;</pre><pre>&lt;div class=&quot;radio&quot; *ngFor=&quot;let gender of genders&quot;&gt;<br>    &lt;label&gt;<br>      &lt;input type=&quot;radio&quot; [value]=&quot;gender&quot; <strong>ngModel</strong> <strong>name=&quot;gender&quot; </strong>/&gt;<br>      {{ gender }}<br>    &lt;/label&gt;<br>  &lt;/div&gt;<br>  &lt;br /&gt;<br>  &lt;button class=&quot;btn btn-primary&quot; type=&quot;submit&quot;&gt;Submit&lt;/button&gt;<br>&lt;/form&gt;</pre><p>အခုလို သတ်မှတ်ပြီးရင် Angular ကတည်ဆောက်ပေးထားတဲ့ JS obj ကို ရယူဖို့ပဲ ကျန်ပါတော့တယ်။ နမူနာအရ Submit button ကိုနှိပ်မယ်ဆိုရင် data ကိုရယူမှာ ဖြစ်ပါတယ်။</p><p>Data ရယူဖို့အတွက် form tag မှာအခုလိုရေးပါ။</p><pre>&lt;form <strong>(ngSubmit)</strong>=&quot;onSubmit(f)&quot; <strong>#f=&quot;ngForm&quot;</strong>&gt;<br>...<br>&lt;/form&gt;</pre><p>ထူးခြားချက်ကတော့ Submit button မှာ (click) event နဲ့ ရယူတာမဟုတ်ပဲ form tag မှာ (ngSubmit) နဲ့ ရယူလိုက်တာ တွေ့ရမှာပါ။ ဒီလိုရယူနိုင်ဖို့အတွက် Submit button က type=submit ပေးထားဖို့ လိုအပ်ပါတယ်။</p><p>#f ဆိုတာ local reference တစ်ခုဖြစ်ပါတယ်။ သူ့ထဲကို assign ထည့်လိုက်တာကတော့ ngForm ပါ။ ngForm ဆိုတာ Angular က အလိုလျှောက် ဆောက်ပေးထားတဲ့ JS Obj ပဲဖြစ်ပါတယ်။</p><p>ဆက်လက်ပြီး typescript ဘက်မှာ အောက်ပါအတိုင်းရေးပါ။</p><pre>onSubmit(form: NgForm) {<br>    console.log(form);<br>}</pre><p>Form မှာ data ဖြည့်ပြီး Submit နှိပ်လိုက်ရင် အခုလို တွေ့ရမှာဖြစ်ပါတယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m1qCwhsQ-MDig_AbQ50r4Q.png" /><figcaption>ngForm Object</figcaption></figure><p>ရရှိလာတဲ့ Obj ထဲက value ကိုဖွင့်ကြည့်လိုက်ရင် form ထဲကဖြည့်လိုက်တဲ့ data တွေကို မြင်တွေ့ရမှာဖြစ်ပါတယ်။ ဒါ့အပြင် တခြားအသုံးဝင်တဲ့ properties တွေလည်းပါဝင်နေတာ မြင်တွေ့ရမှာပါ။ ဥပမာ valid, invalid, touched တို့ကိုသုံးပြီး form valid ဖြစ်မဖြစ် အလွယ်တကူ စစ်နိုင်မှာပါ။</p><h3>Form Validation</h3><p>Form validation လုပ်ဖို့အတွက် Username နဲ့ Email တို့ရဲ့ input field တွေမှာ အခုလိုရေးပါ။</p><pre>&lt;input type=&quot;text&quot; class=&quot;form-control&quot; ngModel name=&quot;username&quot; <strong>required</strong> /&gt;</pre><pre>&lt;input type=&quot;email&quot; class=&quot;form-control&quot; ngModel name=&quot;email&quot;<br><strong>required email</strong>/&gt;</pre><blockquote>တခြားသော validators များကို <a href="https://angular.io/api?query=forms&amp;type=directive"><strong>ဒီမှာ </strong></a>လေ့လာပါ။</blockquote><p>Form ကို ဘာ data မှမဖြည့်ပဲ Submit နှိပ်ရင် valid: false လို့ပြနေမှာ ဖြစ်ပါတယ်။ Username နဲ့ Email တို့ကို မှန်ကန်စွာဖြည့်မှ valid: trueဖြစ်စေမှာပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*27Lms8Kl-ve8IhO3mtL2xg.png" /><figcaption>Form with invalid value</figcaption></figure><p>Form ကို valid ဖြစ်မှ Submit လုပ်ခွင့်ပြုစေဖို့ အခုလို ရေးလိုက်ပါ။</p><pre>&lt;button class=&quot;btn btn-primary&quot; type=&quot;submit&quot; [disabled]=&quot;<strong>f.invalid</strong>&quot;&gt; Submit &lt;/button&gt;</pre><p>f.invalid ကိုစစ်ပြီး ရေးထားတာ ဖြစ်ပါတယ်။ ဒီနေရာမှာ !f.valid လို့သုံးချင်လည်းရပါတယ်။ ရလဒ် အနေနဲ့ data မှန်တော့မှ Submit နှိပ်လို့ရမှာ ဖြစ်ပါတယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*NxQ9OX1n8JzbakgzoPZBTg.gif" /><figcaption>Form with Validation</figcaption></figure><h3>Outputting Validation Error</h3><p>Field တစ်ခုဆီမှာ မှားယွင်းတဲ့ data ထည့်ရင် အသုံးပြုသူကို မှားယွင်းနေကြောင်းအသိပေးနိုင်ဖို့ အောက်ပါအဆင့်တွေ လုပ်နိုင်ပါတယ်။</p><p>Angular က Form control လုပ်ဖို့ JS Obj ဆောက်ပေးတဲ့အပြင် field တစ်ခုချင်းမှာလည်း သက်ဆိုင်ရာ css class တွေကို အလိုလျှောက် ထည့်ပေးပါသေးတယ်။</p><p>Input filed တစ်ခုကို inspect လုပ်ကြည့်ရင် အခုလို တွေ့ရမှာပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/907/1*9XND2OaSX8mTbwpeM3ff2Q.png" /><figcaption>Username’s input with invalid data</figcaption></figure><p>မှန်ကန်တဲ့ data ထည့်လိုက်ရင် အခုလို တွေ့ရမှာပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/911/1*PgNoNqq1YhP94_VjwfrFfQ.png" /><figcaption>Username’s input with valid data</figcaption></figure><p>Input field တွေကို မျိုးစုံစမ်းကြည့်ပါ css class တွေပြောင်းသွားတာ တွေ့ရမှာပါ။ ဒီလိုပြောင်းသွားတာကို အသုံးချဖို့ scss file မှာ အောက်ပါအတိုင်း ရေးလိုက်ပါ။</p><pre>input.ng-touched.ng-invalid {<br>  border: 1px solid red;<br>}</pre><p>ဒါဆိုရင်တော့ cursor ချပြီး data အမှန်မထည့်ရင် ယခုလို ပြမှာ ဖြစ်ပါတယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/973/1*wT6qna6f8vsD1ng_arKx9w.gif" /><figcaption>Form with invalid inputs</figcaption></figure><p>ဆက်လက်ပြီး သက်ဆိုင်ရာ error message ပြပေးဖို့အတွက် အောက်ပါ code တွေရေးပါ။</p><p>Username အတွက် ဒီလိုရေးပါ။</p><pre>&lt;input<br>      type=&quot;text&quot;<br>      class=&quot;form-control&quot;<br>      ngModel<br>      name=&quot;username&quot;<br>      required<br>      <strong>#username=&quot;ngModel&quot;</strong><br>    /&gt;<br>&lt;span class=&quot;form-text&quot; <br>      *ngIf=&quot;<strong>username.invalid &amp;&amp; username.touched</strong>&quot;<br>    &gt;<br>    Please enter a username!&lt;/span&gt;</pre><p>local reference ထဲကို ngModel ထည့်လိုက်ချင်းဖြင့် field တစ်ခုချင်းဆီကို control လုပ်လို့ ရသွားစေပါတယ်။</p><p>Email အတွက်လည်း ဆင်တူပဲဖြစ်ပါတယ်။</p><pre>&lt;input<br>      type=&quot;email&quot;<br>      class=&quot;form-control&quot;<br>      ngModel<br>      name=&quot;email&quot;<br>      required<br>      email<br>      <strong>#email=&quot;ngModel&quot;</strong><br>    /&gt;<br>&lt;span class=&quot;form-text&quot; <br>      *ngIf=&quot;<strong>email.invalid &amp;&amp; email.touche</strong>d&quot;<br>      &gt;Please enter a valid email!&lt;/span&gt;</pre><p>ဒါဆိုရင်တော့ အခုလို ရလဒ်မျိုးရစေမှာပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/956/1*Ht6wTOWkoJf_6isqwdM0Wg.png" /><figcaption>Form with error messages</figcaption></figure><p>ဒီနည်းနဲ့ input field တစ်ခုချင်းဆီကို ထိန်းချုပ်နိုင်မှာ ဖြစ်ပါတယ်။</p><h3>Set Default Values</h3><p>အခုဆိုရင် form ရဲ့ select box နဲ့ radio button တို့မှာ default အနေနဲ့ ရွေးပေးထားတာမရှိသေးပါ။</p><p>Default သတ်မှတ်ပေးနိုင်ဖို့ အောက်ပါအတိုင်း ရေးလိုက်ပါ။</p><pre><strong><em>TypeScript<br></em></strong>defaultSecret = &#39;pet&#39;;<br>defaultGender = &#39;male&#39;;</pre><pre><strong><em>HTML</em></strong><br>&lt;select class=&quot;form-control&quot; <strong>[ngModel]=&quot;defaultSecret&quot; </strong>name=&quot;secret&quot;&gt;...&lt;/select&gt;</pre><pre>&lt;input type=&quot;radio&quot; [value]=&quot;gender&quot; name=&quot;gender <strong>[ngModel]=&quot;defaultGender&quot;</strong> /&gt;</pre><p>ဒါဆိုရင် အခုလိုရလဒ်မျိုး ရစေမှာပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/914/1*ko9gFyopjQetVb3OPe7L3g.png" /><figcaption>Form with default values</figcaption></figure><h3>Getting Form Data</h3><p>ဆက်လက်ပြီး form က data တွေကို ရယူဖို့အတွက် အောက်ပါတို့ကို ရေးပါ။</p><pre>onSubmit(form: NgForm) {<br>    console.log(form);<br>    console.log(&#39;Username: &#39; + form.value.username);<br>    console.log(&#39;Email: &#39; + form.value.email);<br>    console.log(&#39;Secret Question: &#39; + form.value.secret);<br>    console.log(&#39;Gender: &#39; + form.value.gender);<br>}</pre><p>ဒါဆိုရင်တော့ Submit လုပ်ရင် အခုလို မြင်တွေ့ရမှာပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/897/1*2V3ICWCzvKE6MmrnBh-2Dw.png" /><figcaption>Form Data</figcaption></figure><h3>Resetting Form Data</h3><p>Submit လုပ်ပြီး form reset လုပ်ချင်ရင် အခုလို ရေးလိုက်ပါ။</p><pre>onSubmit(form: NgForm) {<br>...<br>form.<strong>reset</strong>();<br>}</pre><p>အကယ်၍ default value တွေပါ မပျက်သွားစေချင်ရင် အခုလို ရေးနိုင်ပါတယ်။</p><pre>onSubmit(form: NgForm) {<br>...<br>form.<strong>reset</strong>({<br>      secret: this.defaultSecret, <br>      gender: this.defaultGender,<br>});<br>}</pre><p>ဒီလောက်ဆိုရင် Tempate-Driven form ကိုအသုံးပြုပြီး control လုပ်လို့ ရပြီပဲ ဖြစ်ပါတယ်။</p><p><strong>Previous: </strong><a href="https://medium.com/learn-ng/built-in-pipes-7a2d0f63ad36"><strong>Built-in Pipes</strong></a></p><p><strong>Next: </strong><a href="https://medium.com/learn-ng/reactive-form-c738fe4142b9"><strong>Reactive Form</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=58cfeebe03fe" width="1" height="1" alt=""><hr><p><a href="https://medium.com/learn-ng/template-driven-form-58cfeebe03fe">Template-Driven Form</a> was originally published in <a href="https://medium.com/learn-ng">Learn Ng</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Built-in Pipes]]></title>
            <link>https://medium.com/learn-ng/built-in-pipes-7a2d0f63ad36?source=rss----23699d95b94c---4</link>
            <guid isPermaLink="false">https://medium.com/p/7a2d0f63ad36</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[learn-angular]]></category>
            <dc:creator><![CDATA[Ye Min Ko]]></dc:creator>
            <pubDate>Tue, 05 Oct 2021 11:30:46 GMT</pubDate>
            <atom:updated>2021-10-06T15:29:26.931Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FVcuyv_S4MRP2j-p" /><figcaption>Photo by <a href="https://unsplash.com/@yajra06?utm_source=medium&amp;utm_medium=referral">Arjay Bernardo</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Data တွေ user ကိုပြသရာမှာ ကြည့်ကောင်းစေဖို့ ပုံစံပြောင်းပြစေချင်ရင် Angular ရဲ့ Pipes တွေကို အသုံးပြုနိုင်ပါတယ်။</p><h3>Date</h3><p>Date တွေကိုထုတ်ပြတဲ့အခါ default အတိုင်းဆိုရင် ကြည့်ရတာအဆင်မပြေပါ။ အောက်ပါအတိုင်း date pipe တွေသုံးပြီး ပြစေချင်တဲ့ format အတိုင်း ပြပေးလို့ရပါတယ်။</p><pre><strong><em>TypeScript</em></strong><br>myDate = new Date();</pre><pre><strong><em>HTML</em></strong><br>&lt;p&gt;Default: {{ myDate }}&lt;/p&gt;<br>&lt;p&gt;Date with pipe: {{ myDate | <strong>date </strong>}}&lt;/p&gt;<br>&lt;p&gt;Date with pipe: {{ myDate | <strong>date</strong>: &quot;medium&quot; }}&lt;/p&gt;<br>&lt;p&gt;Date with pipe: {{ myDate | <strong>date</strong>: &quot;MM-dd-yy&quot; }}&lt;/p&gt;</pre><p>ရလဒ်တွေအနေနဲ့ ယခုလို တွေ့ရမှာ ဖြစ်ပါတယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/689/1*zSA2FvFyhCE81VFaP0YC9Q.png" /><figcaption>Date Pipes</figcaption></figure><p>Date Pipe ရဲ့ အသေးစိတ်ကို <a href="https://angular.io/api/common/DatePipe"><strong>ဒီမှာ</strong></a> ဆက်လက်လေ့လာလို့ရပါတယ်။</p><h3>Text transforms</h3><p>Text တွေကို ပြစေချင်တဲ့ပုံစံ ပြောင်းပြချင်ရင် သုံးနိုင်ပါတယ်။</p><pre>&lt;p&gt;{{ &quot;hello world&quot; | <strong>uppercase</strong> }}&lt;/p&gt;<br>&lt;p&gt;{{ &quot;hello world&quot; | <strong>lowercase</strong> }}&lt;/p&gt;<br>&lt;p&gt;{{ &quot;hello world&quot; | <strong>titlecase </strong>}}&lt;/p&gt;</pre><p>ရလဒ် အနေနဲ့အခုလိုတွေ့ရမှာပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/1*X8SKwX2AgvxWLTbg0TTrgw.png" /><figcaption>Text transforms Pipes</figcaption></figure><h3>Currency</h3><pre>&lt;p&gt;{{ 5000 | <strong>currency</strong>: &quot;MMK&quot; }}&lt;/p&gt; </pre><pre>Output: MMK 5,000</pre><h3>Multiple Pipes</h3><p>Pipe တွေကိုတစ်ခုထက်မက ချိတ်ဆက် သုံးနိုင်ပါတယ်။</p><pre>&lt;p&gt;{{ myDate | <strong>date</strong>: &quot;fullDate&quot; | <strong>uppercase</strong> }}&lt;/p&gt;</pre><pre>Output: TUESDAY, OCTOBER 5, 2021</pre><h3>Multiple Params</h3><p>Pipe တစ်ခုထဲမှာ parameter တစ်ခုထပ်မက ထည့်ပေးလို့ ရပါတယ်။</p><pre>&lt;p&gt;{{ 5000 | <strong>currency</strong>: &quot;EUR&quot; : &quot;code&quot; }}&lt;/p&gt;</pre><pre>Output: EUR5,000.00</pre><h3>Async Pipe</h3><p>Async code တွေကို html ကနေ ခေါ်ချင်ရင် သုံးလို့ရပါတယ်။ Observable တွေကိုလည်း ခေါ်သုံးနိုင်ပါတယ်။ AppComponent မှာ အခုလို စမ်းကြည့်ပါ။</p><pre><strong><em>TypeScript</em></strong><br>items = this.shopService.getProducts();</pre><pre><strong><em>HTML</em></strong><br>&lt;div *ngFor=&quot;let item of items | <strong>async</strong>&quot;&gt;<br>   {{ item.title }}<br>&lt;/div&gt;</pre><p>Async Pipe ကိုသုံးမယ်ဆိုရင် auto subscribe လုပ်ပေးပြီး auto unsubscribe လုပ်ပေးပါတယ်။ ရလဒ်အနေနဲ့ အခုလို မြင်ရမှာပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1020/1*P7MYT4_q1AbtPWChuETygw.png" /><figcaption>Data from Fake Shop APIs</figcaption></figure><p>ဒီလောက်ဆိုရင် pipes တွေကို အသုံးပြုနိုင်ပြီပဲ ဖြစ်ပါတယ်။ တခြား built-in pipes တွေကို <a href="https://angular.io/api/common#pipes"><strong>ဒီမှာ</strong></a><strong> </strong>ဆက်လက် လေ့လာနိုင်ပါတယ်။</p><p><strong>Previous: </strong><a href="https://medium.com/learn-ng/http-requests-ab27d76c5bfd"><strong>Http Requests</strong></a></p><p><strong>Next: </strong><a href="https://medium.com/learn-ng/template-driven-form-58cfeebe03fe"><strong>Template-Driven Form</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7a2d0f63ad36" width="1" height="1" alt=""><hr><p><a href="https://medium.com/learn-ng/built-in-pipes-7a2d0f63ad36">Built-in Pipes</a> was originally published in <a href="https://medium.com/learn-ng">Learn Ng</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Http Requests]]></title>
            <link>https://medium.com/learn-ng/http-requests-ab27d76c5bfd?source=rss----23699d95b94c---4</link>
            <guid isPermaLink="false">https://medium.com/p/ab27d76c5bfd</guid>
            <category><![CDATA[learn-angular]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Ye Min Ko]]></dc:creator>
            <pubDate>Tue, 05 Oct 2021 09:30:23 GMT</pubDate>
            <atom:updated>2021-10-05T11:31:31.233Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*gWQbzOmuo7ts8pWm" /><figcaption>Photo by <a href="https://unsplash.com/@plhnk?utm_source=medium&amp;utm_medium=referral">Paul Hanaoka</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Angular project မှာ Http request တွေ ပြုလုပ်ချင်တယ်ဆိုရင် HttpClient ကိုအသုံးပြုပြီး ရေးသားနိုင်ပါတယ်။</p><p>အရင်ဦးဆုံး request တွေမရေးခင် AppModule ထဲမှာ HttpClientModule ကို import လုပ်ပေးဖို့ လိုအပ်ပါတယ်။</p><pre>import { <strong>HttpClientModule </strong>} from &#39;@angular/common/http&#39;;</pre><pre>@NgModule({<br>...</pre><pre>imports: [..., <strong>HttpClientModule</strong>],</pre><pre>})<br>export class AppModule {}</pre><h3>GET</h3><p>Data တွေ တောင်းယူနိုင်ဖို့အတွက် Get request ကိုအသုံးပြုနိုင်ပါတယ်။ အရင်ဆုံး ShopService ကို generate လုပ်ပါ။ ပြီးရင် အောက်ပါအတိုင်းရေးပါ။</p><p>စမ်းသပ်ခေါ်ယူမဲ့ API တွေက <a href="https://fakestoreapi.com/docs"><strong>Fake Store API</strong></a> ကနေ အသုံးပြုထားတာ ဖြစ်ပါတယ်။</p><pre>export class ShopService {<br>  constructor(private httpClient: <strong>HttpClient</strong>) {}</pre><pre>getProducts(): Observable&lt;any&gt; {<br>    const url = &#39;https://fakestoreapi.com/products&#39;;<br>    const headers = new <strong>HttpHeaders</strong>({<br>      myData: &#39;Something&#39;,<br>    });<br>    const params = new <strong>HttpParams</strong>().set(&#39;limit&#39;, 5);<br>    return this.httpClient.<strong>get&lt;any&gt;</strong>(url, { headers, params });<br>}</pre><p>Request တွေခေါ်ယူဖို့ HttpClient ကို inject လုပ်ထားပါတယ်။ get() နဲ့ GET request ပြုလုပ်နိုင်ပါတယ်။</p><p>get() ရဲ့ first param မှာ URL ကိုထည့်နိုင်ပြီး၊ second param မှာတော့ options တွေထည့်ပေးနိုင်ပါတယ်။</p><p>GET request တစ်ခုမှာအနည်းဆုံး URL ပါရင် ရပြီဖြစ်ပါတယ်။ အကယ်၍ return type သတ်မှတ်ချင်ရင်လည်း သတ်မှတ်နိုင်ပါတယ်။ နမူနာမှာတော့ get&lt;any&gt;() လို့သတ်မှတ်ထားပါတယ်။</p><p>Headers တွေသတ်မှတ်ချင်ရင် HttpHeaders နဲ့ သတ်မှတ်နိုင်ပါတယ်။ HttpHeaders သည် “Content-Type”: “application/json” ကို default အဖြစ် ထည့်ပေးပြီးသား ဖြစ်ပါတယ်။</p><p>Query params တွေထည့်ချင်ရင် HttpParams နဲ့ထည့်နိုင်ပါတယ်။</p><p>ဆက်လက်ပြီး AppComponent မှာအခုလိုရေးပါ။</p><pre>getProducts() {<br>    this.shopService.getProducts().subscribe((data) =&gt; {<br>      console.log(data);<br>    });<br>  }</pre><pre>ngOnInit() {<br>    this.getProducts();<br>}</pre><p>သတိပြုရန်မှာ HttpClient ကို subscribe လုပ်ပြီးသုံးရင် unsubscribe လုပ်ပေးစရာ မလိုပါ။ Angular က အလိုလျောက် လုပ်ပေးလို့ ဖြစ်ပါတယ်။</p><p>စမ်းကြည့်ရင်တော့ အခုလိုရလာမှာဖြစ်ပါတယ်။ Network tab မှာလည်း request ကိုလေ့လာနိုင်ပါတယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zPpIORNf9L6uD13JStVtlQ.png" /><figcaption>data from Fake Store API</figcaption></figure><p>ဒီနည်းနဲ့ GET request ကိုပြုလုပ်နိုင်မှာဖြစ်ပါတယ်။</p><h3>POST</h3><p>Get ကိုသိရင် post ကိုအသုံးပြုရတာ လွယ်ကူစေမှာ ဖြစ်ပါတယ်။ ShopService မှာ method တစ်ခု ထပ်ရေးပါ။</p><pre>addProduct(data: any): Observable&lt;any&gt; {<br>    const url = &#39;https://fakestoreapi.com/products&#39;;<br>    return this.httpClient.<strong>post</strong>&lt;any&gt;(url, <strong>data</strong>);<br>}</pre><p>post() method မှာတော့ body ကို second param မှာ ထည့်ပေးရပါတယ်။ Options တွေ ထပ်ထည့်ချင်ရင် third param မှာ ထည့်နိုင်ပါတယ်။</p><p>AppComponent မှာ အခုလိုထပ်ရေးပြီး စမ်းကြည့်ပါ။</p><pre>addProduct() {<br>    let data = {<br>      title: &#39;Test product&#39;,<br>      price: 1300,<br>    };<br>    this.shopService.addProduct(data).subscribe((response) =&gt; {<br>      console.log(response);<br>    });<br>  }</pre><pre>ngOnInit() {<br>    this.addProduct();<br>}</pre><p>PUT နဲ့ PATCH တို့ကလည်း POST နဲ့အတူတူပဲ ဖြစ်လို့ ကိုယ်တိုင် စမ်းသပ်ကြည့်ပါ။</p><blockquote><strong>POST </strong>— Data အသစ်တွေ ပေးပို့ဖို့သုံး။</blockquote><blockquote><strong>PUT </strong>— Data အဟောင်းကို Data အသစ်နဲ့ အစားထိုးဖို့သုံး။</blockquote><blockquote><strong>PATCH </strong>— Data အဟောင်းထဲက တစ်စိတ်တစ်ပိုင်းကိုပဲ ပြင်ဖို့သုံး။</blockquote><blockquote><em>(</em>Reference: <a href="https://eimaung.com/api/">API လို-တို-ရ</a>ှင်း<em>)</em></blockquote><h3>DELETE</h3><p>ShopService မှာအခုလိုရေးပါ။</p><pre>deleteProduct(id: number): Observable&lt;any&gt; {<br>    const url = `https://fakestoreapi.com/products/${id}`;<br>    return this.httpClient.<strong>delete</strong>&lt;any&gt;(url);<br>}</pre><p>AppComponent ကနေခေါ်သုံးပါ။</p><pre>deleteProduct() {<br>    this.shopService.deleteProduct(1).subscribe((response) =&gt; {<br>      console.log(response);<br>    });<br>  }</pre><pre>ngOnInit() {<br>    this.deleteProduct();<br>}</pre><p>ဒီလောက်ဆိုရင် Http requests တွေကို အလွယ်တကူ ခေါ်ယူ အသုံးပြုနိုင်ပြီပဲ ဖြစ်ပါတယ်။</p><p><strong>Previous: </strong><a href="https://medium.com/learn-ng/useful-built-in-observables-6e665ab483a0"><strong>Useful Built-in Observables</strong></a></p><p><strong>Next: </strong><a href="https://medium.com/learn-ng/built-in-pipes-7a2d0f63ad36"><strong>Built-in Pipes</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ab27d76c5bfd" width="1" height="1" alt=""><hr><p><a href="https://medium.com/learn-ng/http-requests-ab27d76c5bfd">Http Requests</a> was originally published in <a href="https://medium.com/learn-ng">Learn Ng</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Useful Built-in Observables]]></title>
            <link>https://medium.com/learn-ng/useful-built-in-observables-6e665ab483a0?source=rss----23699d95b94c---4</link>
            <guid isPermaLink="false">https://medium.com/p/6e665ab483a0</guid>
            <category><![CDATA[learn-angular]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Ye Min Ko]]></dc:creator>
            <pubDate>Thu, 30 Sep 2021 15:13:52 GMT</pubDate>
            <atom:updated>2021-10-05T09:31:04.281Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Bd9L1I7WtAXpkC0P" /><figcaption>Photo by <a href="https://unsplash.com/@abe_b_ryokan?utm_source=medium&amp;utm_medium=referral">Abe B. Ryokan</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Subject</h3><p>Event တစ်ခု ဖြစ်ပေါ်လာတိုင်း တစ်ခုခုကို လုပ်စေချင်ရင် Observable မှာ Subject ဆိုတာရှိပါတယ်။ EventEmitter နဲ့အတူတူပါပဲ Subject ကပိုကောင်းတဲ့နည်းလမ်းဖြစ်လို့ အစားထိုးသုံးသင့်ပါတယ်။</p><p>အရင်ဆုံး EventEmitter နဲ့စမ်းကြပါမယ်။ UserService ထဲမှာ အခုလိုရေးပါ။</p><pre>activatedEmitter = new EventEmitter&lt;boolean&gt;();</pre><p>UsersComponent ထဲမှာ button တစ်ခုထည့်လိုက်ပါ။</p><pre><strong><em>HTML</em></strong><br>&lt;button <em>class</em>=&quot;btn btn-primary&quot; <em>(click)</em>=&quot;<strong>activate</strong>()&quot;&gt;Activate Me&lt;/button&gt;</pre><pre><strong><em>TypeScript</em></strong><br>activate() {<br>    this.userService.activatedEmitter.<strong>emit</strong>(true);<br>}</pre><p>AppComponent ကနေ subscribe လုပ်ပြီး true ဖြစ်မှ စာပြပေးအောင် ရေးလိုက်ပါ။</p><pre><strong><em>HTML</em></strong><br>&lt;h1 <em>*ngIf</em>=&quot;isActivated&quot;&gt;Activated!&lt;/h1&gt;</pre><pre><strong><em>TypeScript</em></strong><br>isActivated = false;</pre><pre>ngOnInit() {<br>    this.userService.activatedEmitter.<strong>subscribe</strong>((didActivate) =&gt; {<br>      this.isActivated = didActivate;<br>    });<br>}</pre><p>ဒါဆိုရင် button နှိပ်လိုက်တာနဲ့ AppComponent ကသိပြီး စာတန်းပြပေးမှာ ဖြစ်ပါတယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jiYQZOnxXqyVFFrHwwJ2ig.png" /><figcaption>localhost:4200</figcaption></figure><p>ဆက်လက်ပြီး EventEmitter နေရာမှာ Subject ဘယ်လိုပြောင်းသုံးမလဲ ကြည့်ရပါမယ်။</p><p>UserService ထဲမှာအခုလိုပြင်ပါ။ EventEmitter နေရာမှာ အစားထိုးလိုက်တာ ပါပဲ။</p><pre>import { Subject } from &#39;rxjs&#39;;</pre><pre>activatedEmitter = new <strong>Subject</strong>&lt;boolean&gt;();</pre><p>UsersComponent ထဲက emitနေရာမှာ next လိုပြောင်းပေးရပါမယ်။</p><pre>this.userService.activatedEmitter.<strong>next</strong>(true);</pre><p>ဒီလိုပြောင်းလိုက်ယုံနဲ့ နဂိုအတိုင်း ပြန်အလုပ်လုပ်သွားမှာ ဖြစ်ပါတယ်။ Subscribe လုပ်ပြီး အသုံးပြုပြီဆိုရင် memory leak မဖြစ်စေဖို့ ngOnDestroy မှာ unsubscribe လုပ်ဖို့မမေ့ပါနဲ့။ AppComponent မှာ အခုလို ထပ်ထည့်ပါ။</p><pre>sub: Subscription<strong> </strong>= new <strong>Subscription</strong>();</pre><pre>ngOnInit() {<br>    this.<strong>sub </strong>= this.userService.activatedEmitter.subscribe(...);<br>  }</pre><pre>ngOnDestroy() {<br>    this.sub.<strong>unsubscribe</strong>();<br>}</pre><p>Subject က Observable ဖြစ်လို့ Operators တွေနဲ့လည်း တွဲသုံးလို့ ရသွားမှာ ဖြစ်ပါတယ်။ တစ်ခုသတိထားရမှာက @Output နဲ့သုံးတဲ့အခါ EvenEmitter ကိုသာသုံးရမှာဖြစ်ပြီး Subject ကို ပြောင်းသုံးလို့ မရပါ။</p><h3>BehaviorSubject</h3><p>Subject နဲ့အတူတူပါပဲ။ ဒါပေမဲ့ BehaviorSubject ရဲ့ထူးခြားချက်က subscribe မလုပ်ခင်ကပြောင်းလဲသွားခဲ့တဲ့ data ကိုရယူနိုင်ခြင်းပဲ ဖြစ်ပါတယ်။</p><p>ဆိုလိုတာက Subject သည် emit လုပ်လိုက်တဲ့ data ကိုမသိမ်းထားပါ။ ဒါကြောင့် subscribe မလုပ်ခင်က emit လုပ်လိုက်တဲ့ data ကို subscribe လုပ်သူက လက်ခံရရှိလိုက်မှာ မဟုတ်ပါ။</p><p>ဥပမာစမ်းသပ်နိုင်ဖို့ AppComponent မှာအခုလိုရေးပါ။</p><pre>subject = new <strong>Subject</strong>&lt;number&gt;();<br>sub2: Subscription = new Subscription();</pre><pre>testSubject() {<br>    this.subject.<strong>next</strong>(1);<br>    this.sub2 = this.subject.subscribe((num) =&gt; {<br>      console.log(&#39;num is : &#39; + num);<br>    });<br>    this.subject.<strong>next</strong>(2);<br>}</pre><pre>ngOnInit() {<br>    this.testSubject();<br>}</pre><pre>ngOnDestroy() {<br>    this.sub2.unsubscribe();<br>}</pre><p>console ကိုကြည့်လိုက်ရင် <em>num is : 2</em> ပဲထွက်လာတာ တွေ့ရမှာပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/815/1*tOTnUjGjpiE0ICYgxQseMA.png" /><figcaption>Subject test</figcaption></figure><p>ဒီလိုဖြစ်ရတာက next(1) ကိုလုပ်ချိန်မှာ Subject ကို subscribe မလုပ်ရသေးတာကြောင့်ဖြစ်ပါတယ်။ next(2) လုပ်ပြီးချိန်မှာတော့ subscribe လုပ်ပြီးပြီဖြစ်လို့ log ထွက်လာတာပဲဖြစ်ပါတယ်။</p><p>ဆက်လက်ပြီး BehaviorSubject ကိုစမ်းသပ်ဖို့ AppComponent မှာပဲဆက်ပြီး အောက်ပါ code တွေရေးပါ။</p><pre>behaviorSubject = new <strong>BehaviorSubject</strong>&lt;number&gt;(<strong>0</strong>);<br>sub3: Subscription = new Subscription();</pre><pre>testBehaviorSubject() {<br>    this.behaviorSubject.<strong>next</strong>(1);<br>    this.sub3 = this.behaviorSubject.subscribe((num) =&gt; {<br>      console.log(&#39;[Behaviour] num is : &#39; + num);<br>    });<br>    this.behaviorSubject.<strong>next</strong>(2);<br>}</pre><pre>ngOnInit() {<br>    this.testBehaviorSubject();<br>}</pre><pre>ngOnDestroy() {<br>    this.sub3.unsubscribe();<br>}</pre><p>BehaviorSubject က initial variable တစ်ခု သတ်မှတ်ပေးဖို့ လိုတာကြောင့် 0 ထည့်ထားတာ တွေ့ရမှာပါ။ ရလဒ်ကတော့ အခုလို တွေ့ရမှာပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/807/1*R3aH7yaEg6zrHlg0zGrkUQ.png" /><figcaption>BehaviorSubject test</figcaption></figure><p>1 ရော 2 ပါလက်ခံရရှိတာ တွေ့ရမှာ ဖြစ်ပါတယ်။ ဒါကြောင့် လက်ရှိ data ကိုလည်းလိုချင်တယ် နောက်ထပ် emit လုပ်မဲ့ data ကိုလည်းလိုချင်ရင် BehaviorSubject ကိုသုံးပါ။ လက်ရှိ data ကိုမလိုချင်ပဲ နောက်ထပ် emit လုပ်မဲ့ data အသစ်ကိုပဲ လိုချင်ရင်တော့ Subject ကို အသုံးပြုနိုင်ပါတယ်။</p><h3>Prevent pushing data to Subjects</h3><p>Subjects (<em>Subject and </em><em>BehaviorSubject</em>) တွေက emit လုပ်နိုင်သလို subscribe လည်းလုပ်နိုင်ပါတယ်။ ကိုယ်ရေးထားတဲ့ Subjects ကို subscribe လုပ်ခွင့်ပဲပေးပြီး emit လုပ်ခွင့် မပေးချင်ရင် အခုလို ကာကွယ်ထားလို့ ရပါတယ်။</p><p>Subject လေ့လာတုံးက ရေးသားခဲ့တာကိုပဲ ပြုပြင်ပြီး လေ့လာကြပါမယ်။ အရင်ဆုံး UserService မှာအခုလိုပြင်ရေးပါ။</p><pre>export class UserService {<br>  <strong>private </strong>activatedEmitter = new Subject&lt;boolean&gt;();<br>  activateObservable = this.activatedEmitter.<strong>asObservable()</strong>;</pre><pre>  doActivate() {<br>      this.activatedEmitter.<strong>next</strong>(true);<br>  }</pre><pre>...<br>}</pre><p>နဂိုရေးသားခဲ့တဲ့ Subject ကို private လုပ်လိုက်ပါတယ်။ ဒါကြောင့် service ပြင်ပက access လုပ်လို့ရတော့မှာ မဟုတ်ပါ။ Subject ကို asObservable() သုံးပြီး Observable တစ်ခုအနေနဲ့ ပြောင်းယူလိုက်ပါတယ်။ ဒါကြောင့် ခေါ်သုံးမဲ့သူတွေဟာ activateObservable ကိုပြောင်းခေါ်ပေးရမှာ ဖြစ်သလို subscribe ပဲလုပ်လို့ရတော့မှာလည်း ဖြစ်ပါတယ်။ ဒါကြောင့် emit လုပ်ဖို့အတွက် service ထဲမှာပဲ method တစ်ခုနဲ့ရေးထားလိုက်တာ ဖြစ်ပါတယ်။</p><p>ဆက်လက်ပြီး UsersComponent မှာအခုလို ပြင်ပေးပါ။</p><pre>activate() {<br>    this.userService.<strong>doActivate</strong>();<br>}</pre><p>ပြီးရင်တော့ AppComponent မှာ အောက်ပါအတိုင်း activatedObservable ကို ပြောင်းခေါ်လိုက်ပါ။</p><pre>ngOnInit() {<br>   this.sub = this.userService.<strong>activateObservable</strong>.subscribe();<br>   ...<br>}</pre><p>ရလဒ်ကတော့ အတူတူပဲဖြစ်ပေမဲ့ Subject ကိုတိုက်ရိုက် access လုပ်တာ မဟုတ်တော့ပဲ Observable ကနေသာ access လုပ်ရတော့တာ ဖြစ်လို့ မှားယွင်းပြီး next() ခေါ်မိမှာတွေကို ကာကွယ်ပြီးသား ဖြစ်သွားပါတယ်။</p><h3>Of</h3><p>Argument တွေကို observable sequence အဖြစ် ပြောင်းလဲပေးတာ ဖြစ်ပါတယ်။ AppComponent မှာ အခုလို စမ်းနိုင်ပါတယ်။</p><pre>ngOnInit() {<br>  <strong>of</strong>(&#39;A&#39;, &#39;B&#39;, &#39;C&#39;, &#39;D&#39;).subscribe((data) =&gt; console.log(data));<br>  <strong>of</strong>([1, 2, 3]).subscribe((data) =&gt; console.log(data));<br>}</pre><p>ရလဒ်အနေနဲ့ အခုလို ထွက်လာမှာ ဖြစ်ပါတယ်။ argument တစ်ခုကို emit တစ်ခါလုပ်ပေးတာ တွေ့ရမှာပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*R_H1ZofwSgtx6mpPWeLWYA.png" /><figcaption>rxjs of</figcaption></figure><h3>From</h3><p>Observable မဟုတ်တာကို Observable အဖြစ် convert လုပ်ပေးပါတယ်။ Promise ကို Observable ပြောင်းရာမှာ အသုံးများပါတယ်။</p><pre>ngOnInit() {<br>  <strong>from</strong>([1, 2, 3]).subscribe((data) =&gt; console.log(data));</pre><pre>  let asyncTask = <strong>new Promise</strong>((resolve, reject) =&gt; resolve(&#39;Hi&#39;));<br>  <strong>from</strong>(asyncTask).subscribe((data) =&gt; console.log(data));<br>}</pre><p>ရလဒ်အနေနဲ့ အခုလို ထွက်လာမှာ ဖြစ်ပါတယ်။ Array ကိုထည့်ပေးလိုက်တဲ့အခါ of နဲ့မတူတဲ့ ရလဒ်ကို ထုတ်ပေးတာ သတိပြုပေးပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QrRFnSPvRIusi6w0yUgbnQ.png" /><figcaption>rxjs from</figcaption></figure><p><strong>Previous: </strong><a href="https://medium.com/learn-ng/operators-56e0f0f2700a"><strong>Operators</strong></a></p><p><strong>Next: </strong><a href="https://medium.com/learn-ng/http-requests-ab27d76c5bfd"><strong>Http Requests</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6e665ab483a0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/learn-ng/useful-built-in-observables-6e665ab483a0">Useful Built-in Observables</a> was originally published in <a href="https://medium.com/learn-ng">Learn Ng</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Operators]]></title>
            <link>https://medium.com/learn-ng/operators-56e0f0f2700a?source=rss----23699d95b94c---4</link>
            <guid isPermaLink="false">https://medium.com/p/56e0f0f2700a</guid>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[learn-angular]]></category>
            <dc:creator><![CDATA[Ye Min Ko]]></dc:creator>
            <pubDate>Wed, 01 Sep 2021 12:20:41 GMT</pubDate>
            <atom:updated>2021-09-30T17:40:30.428Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2EsWmrHwL_k22A0u" /><figcaption>Photo by <a href="https://unsplash.com/@alexkixa?utm_source=medium&amp;utm_medium=referral">Alexandre Debiève</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Observable ကနေရရှိလာတဲ့ data ကို တန်းလက်မခံပဲ လိုချင်တဲ့ပုံစံ ပြောင်းပြီးမှ ရယူချင်တယ်ဆိုရင် Operators တွေကို အသုံးပြုနိုင်ပါတယ်။</p><h3>Filter</h3><p>Operators ရဲ့ အသုံးဝင်ပုံကို မြင်သာစေဖို့အတွက် အခုလို ရေးကြပါမယ်။ HomeComponent ရဲ့ ngOnInit() မှာ အခုလိုထပ်ရေးပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/872/1*ljGU3fYGhKJN5ClU3ZDRCQ.png" /><figcaption>rxjs filter</figcaption></figure><p>Operators တွေအသုံးပြုဖို့အတွက် pipe() ကိုသုံးရပါတယ်။ subscribe() ရဲ့ရှေ့မှာရေးပေးရပါတယ်။ pipe() ထဲမှာအသုံးပြုချင်တဲ့ operators တွေကို rxjs/operators ကနေ import လုပ်ပေးဖို့လိုပါတယ်။ အခုက filter ကိုသုံးမှာဖြစ်လို့ အခုလို import လုပ်ပေးပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/771/1*aHcxPuriPE8KDM6PFEyYDA.png" /><figcaption>import for filter</figcaption></figure><p>filter ထဲမှာတော့ count 0 မဖြစ်တာပဲ ပေးဖို့ရေးထားပါတယ်။ ဒါကြောင့် ပြန်စမ်းကြည့်ရင် 0 မပါတော့ပဲ 1 ကနေပဲစတော့မှာ ဖြစ်ပါတယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ygh-NyaJ4CMFs_Ne2bX30w.png" /><figcaption>localhost logs</figcaption></figure><h3>Map</h3><p>pipe() မှာ တစ်ခုထပ်မက operator တွေကို ရေးသားနိုင်ပါတယ်။ လက်တွေ့ လုပ်ကြည့်နိုင်ဖို့အတွက် အခုလို ထပ်ရေးပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/955/1*g_2AQRfjXC3NinHxZVyKUA.png" /><figcaption>rxjs map</figcaption></figure><p>import ကိုတော့ အခုလိုရေးပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/855/1*IfNOLj9l0TddNHRcOfJvdw.png" /><figcaption>import for map</figcaption></figure><p>ခုရေးလိုက်တာကတော့ filter လုပ်လို့ရလာတဲ့ data ကိုမှ map နဲ့ပြုပြင်လိုက်တာ ဖြစ်ပါတယ်။ ရလဒ်အနေနဲ့ အခုလို ဖြစ်သွားမှာ ဖြစ်ပါတယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8-tUQ6T5Bcrt5NOOeg6IbQ.png" /><figcaption>localhost’s logs</figcaption></figure><p>ဒီလိုမျိုး operators တွေကို အသုံးပြုပြီး ကိုယ်လိုချင်တဲ့ ပုံစံဖြစ်အောင် ပြောင်းလဲရယူနိုင်မှာ ဖြစ်ပါတယ်။</p><h3>Tap</h3><p>Pipe ထဲမှာ လက်ခံရရှိတဲ့ data ကို log ထုတ်ကြည့်ချင်တဲ့အခါ၊ ဒါမှမဟုတ် operators တွေသုံးပြီး data ကို ပြုပြင်လိုက်ရင် ဘယ်လိုပုံစံ ထွက်လာလဲ စစ်ချင်တဲ့အခါ အသုံးပြုနိုင်ပါတယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*jB1DhtHdAN8N78h6uYvixw.png" /><figcaption>rxjs tap</figcaption></figure><p>နမူနာမှာဆိုရင် မပြုပြင်ရသေးတဲ့ data နဲ့ ပြုပြင်ပြီး data ကို log ထုတ်ထားတာ တွေ့ရမှာပါ။ ဒီနည်းနဲ့ အလွယ်တကူ debug လုပ်နိုင်ပါတယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/810/1*UdkoOoQEqe90KPk84_6pgg.png" /><figcaption>console log</figcaption></figure><h3>Take</h3><p>Observable မှ emit ဘယ်နှကြိမ်ထိ ယူမယ်ဆိုတာ သတ်မှတ်ဖို့ သုံးပါတယ်။ ဥပမာ customInterval မှ emit ၃ ကြိမ်ထိပဲ ယူမယ်ဆိုရင် အခုလို ရေးနိုင်ပါတယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1007/1*ToIAn4q0XOIjnpBDfSfKUg.png" /><figcaption>rxjs take</figcaption></figure><h3>CatchError</h3><p>Observable က error emit လုပ်လိုက်ရင် တခြား observable ကို ပြောင်းခေါ်စေချင်ရင်ဖြစ်စေ၊ retry ပြန်လုပ်စေချင်ရင်ဖြစ်စေ၊ error ကို ပုံစံပြောင်းချင်ရင် ဖြစ်စေ သုံးနိုင်ပါတယ်။</p><h4>တခြား Observable သို့ ပြောင်းခေါ်ပေးခြင်း</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/976/1*ZZwca2o4zhXoRzZPfxJf7Q.png" /><figcaption>rxjs catchError — 1</figcaption></figure><p>Error တက်ရင် interval() ကို ပြောင်းခေါ်လိုက်တာ ဖြစ်ပါတယ်။ take(10) ဖြစ်လို့ total 10 ကြိမ်ပဲ emit လုပ်စေပါတယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/814/1*rPkKW4undciwr2szJCTaoQ.png" /><figcaption>console logs</figcaption></figure><h4>Error ဖြစ်ရင် retry ပြန်လုပ်စေခြင်း</h4><p>Second parameter ကို return ပြန်လိုက်ရင် observable ကို ပြန်ခေါ်ပေးစေပါတယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/806/1*QY-BBDi7SHHApfEfYvxb_Q.png" /><figcaption>rxjs catchError — 2</figcaption></figure><p>Console မှာ ကြည့်ရင် အခုလို တွေ့ရမှာပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/809/1*UgsRUVNOoHdAnu1pZfyM6g.png" /><figcaption>console logs</figcaption></figure><h4>Error လက်ခံပြီး error အသစ်တစ်ခု ထုတ်ပေးခြင်း</h4><p>အခုလိုမျိုး error အသစ်ပြောင်းပြီး ထုတ်ပေးလို့လည်းရတယ်။ subscribe ထဲက error handler ထဲမှာ error.message ကို error လို့ပဲထားလိုက်ပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/887/1*0Pk12W0u7GL7ZLtoK9Ht-g.png" /><figcaption>rxjs catchError — 3</figcaption></figure><p>ဒါဆိုရင်အခုလို error message ပြောင်းသွားတာ တွေ့ရမှာပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/548/1*saoTCwmQTtXlMABD5wgkIQ.png" /><figcaption>alert box</figcaption></figure><h3>ExhaustMap</h3><p>Observable တစ်ခုက emit လုပ်လိုက်တဲ့ data ကိုသုံးပြီး တခြား observable ကိုခေါ်ချင်ရင် သုံးနိုင်ပါတယ်။ HomeComponent မှာပဲ method တစ်ခု ထပ်ရေးပြီး ngOnInit ကခေါ်ပေးလိုက်ပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/962/1*GarWVy0M-aDsKuG_lNxtMw.png" /><figcaption>rxjs exhaustMap</figcaption></figure><p>ဒီမှာဆိုရင် customInterval က emit တစ်ခါလုပ်ရင် interval ဆီက emit ၃ ခါယူဖို့ရေးထားတာတွေ့ရမှာပါ။ ရလဒ်အနေနဲ့အခုလိုထွက်လာပါတယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/808/1*ne4UO3uVd6aRcqemALvfMA.png" /><figcaption>console logs</figcaption></figure><p>သတိထားရမှာကတော့ exhaustMap သည် သူ့ထဲက observable ကို complete ဖြစ်သည်ထိ စောင့်ပါတယ်။ complete မဖြစ်မချင်း အပြင်က ထပ်ဝင်လာတဲ့ data အသစ်ကို လက်ခံမှာမဟုတ်ပါ။</p><p>ဒါကြောင့် Form တွေကို submit လုပ်တဲ့အခါ user က submit button ကိုအကြိမ်ကြိမ်နှိပ်လို့ http request တွေ အများကြီး ထွက်သွားမှာကို ကာကွယ်ချင်တဲ့အခါ exhaustMap ကို အသုံးပြုလို့ ရပါတယ်။</p><p>RxJS မှာ build-in operators များစွာရှိပါတယ်။ အသေးစိတ်လေ့လာလိုပါက အောက်ပါတို့မှာ လေ့လာပါ။</p><ul><li><a href="https://www.learnrxjs.io/">Introduction | Learn RxJS</a></li><li><a href="https://academind.com/tutorials/understanding-rxjs/">Understanding RxJS</a></li><li><a href="https://academind.com/tutorials/rxjs-6-what-changed/">RxJS 6 - What Changed?</a></li></ul><p><strong>Previous: </strong><a href="https://medium.com/learn-ng/working-with-custom-observable-853ffb42787c"><strong>Working with Custom Observable</strong></a></p><p><strong>Next: </strong><a href="https://medium.com/learn-ng/useful-built-in-observables-6e665ab483a0"><strong>Useful Built-in Observables</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=56e0f0f2700a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/learn-ng/operators-56e0f0f2700a">Operators</a> was originally published in <a href="https://medium.com/learn-ng">Learn Ng</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Working with Custom Observable]]></title>
            <link>https://medium.com/learn-ng/working-with-custom-observable-853ffb42787c?source=rss----23699d95b94c---4</link>
            <guid isPermaLink="false">https://medium.com/p/853ffb42787c</guid>
            <category><![CDATA[learn-angular]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Ye Min Ko]]></dc:creator>
            <pubDate>Wed, 01 Sep 2021 10:36:37 GMT</pubDate>
            <atom:updated>2021-09-01T12:21:46.282Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2petK25PVhgVxLdm" /><figcaption>Photo by <a href="https://unsplash.com/@lefterisk?utm_source=medium&amp;utm_medium=referral">Lefteris kallergis</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Building a Custom Observable</h3><p>Observable အသုံးပြုပုံကို interval() method နဲ့စမ်းသပ် လေ့လာခဲ့ပြီး ဖြစ်ပါတယ်။ ဒီတစ်ခါမှာတော့ ကိုယ်ပိုင် Observable method တစ်ခုကို ဘယ်လိုရေးသားရမလဲဆိုတာ လေ့လာရမှာပါ။</p><p>HomeComponent မှာ အခုလို ရေးလိုက်ပါ။ interval() ကိုပဲ ကိုယ်တိုင် custom method ရေးလိုက်တာ ဖြစ်ပါတယ်။</p><pre>customInterval() {<br>    return <strong>new Observable</strong>((observer) =&gt; {<br>      let count = 0;<br>      setInterval(() =&gt; {<br>        <strong>observer.next(count);</strong><br>        count++;<br>      }, 1000);<br>    });<br> }</pre><p>Observable တစ်ခုကို new နဲ့အခုလို ဆောက်ရပါတယ်။ rxjs ကနေ import လုပ်ပေးဖို့မမေ့ပါနဲ့။ callback function ထဲမှာ code တစ်ချို့ရေးပြီး observer အတွက် လိုအပ်တဲ့ data ထုတ်ပေးဖို့ observer.next() နဲ့ emit လုပ်လိုက်ပါတယ်။</p><p>ဒီလိုထုတ်ပေးရာမှာ next(), error(), complete() ဆိုပြီး ၃ မျိုးရှိပါတယ်။</p><p>next() က data emit လုပ်ဖို့သုံးပါတယ်။</p><p>error() က error ဖြစ်ကြောင်း emit လုပ်ဖို့သုံးပါတယ်။ error ဖြစ်သွားရင် emit လုပ်တာ <strong>လုံးဝ</strong> ရပ်သွားမှာဖြစ်ပါတယ်။ ဆက် emit လို့ မရတော့ပါ။</p><p>complete() ကတော့ data emit လုပ်ခြင်း ပြီးဆုံးကြောင်း data ထပ် emit လုပ်မှာမဟုတ်တော့ကြောင်း ပြောဖို့အတွက် သုံးပါတယ်။</p><p>ခေါ်သုံးဖို့ကတော့ ngOnInit() မှာ အခုလို ရေးလိုက်ပါ။</p><pre>ngOnInit(): void {<br>    // this.firstSub = interval(1000).subscribe((count: number) =&gt; {<br>    //   console.log(count);<br>    // });</pre><pre>    this.firstSub = this.customInterval().<strong>subscribe</strong>((count) =&gt; {<br>          console.log(count);<br>    });<br>}</pre><p>အရင် interval() သုံးခဲ့တာကို comment ပိတ်လိုက်ပြီး customInterval() ကိုပြောင်းခေါ်လိုက်ပါ။ ပြီးရင်တော့ ပြန်စမ်းကြည့်ရင် အရင်အတိုင်းဆက်ပြီး အလုပ်လုပ်နေတာကို တွေ့ရမှာပါ။</p><p>ဒါပေမဲ့ ဒီတစ်ခါကတော့ ကိုယ်တိုင်ရေးသားထားတဲ့ Observable နဲ့ အသုံးပြုလိုက်ခြင်းပဲဖြစ်ပါတယ်။</p><h3>Errors and Completion</h3><p>Observable ကနေnext() နဲ့ data emit လုပ်နိုင်ကြောင်း သိပြီးတဲ့နောက် error နဲ့ completion တွေကိုဘယ်လို emit လုပ်ပြီး ဘယ်လို handle လုပ်မလဲဆိုတာ ဆက်လေ့လာရပါမယ်။</p><p>အရင်ဆုံး error emit လုပ်ခြင်းနဲ့ handle လုပ်ခြင်းကို စမ်းသပ်ပါမယ်။</p><p>customInterval() ရဲ့ setInterval() ထဲမှာ အခုလို ထပ်ထည့်ပါ။</p><pre>setInterval(() =&gt; {<br>  if (count &gt; 3) <br>    <strong>observer.error(new Error(&#39;Count is greater than 3!&#39;));</strong><br>  observer.next(count);<br>  count++;<br>}, 1000);</pre><p>count 3 ထပ်ကျော်သွားရင် error emit လုပ်လိုက်တာပါ။ သတိထားရမှာကတော့ error emit လုပ်လိုက်ရင် observable ရပ်သွားပြီး emit ဆက်လုပ်လို့ရမှာ မဟုတ်တော့ပါ။</p><p>error ကို handle လုပ်ဖို့ကတော့ ngOnInit() ထဲမှာ အခုလို ပြင်လိုက်ပါ။</p><pre>this.firstSub = this.customInterval().subscribe(<br>      (count) =&gt; {<br>        console.log(count);<br>      },<br>      <strong>(error) =&gt; {<br>        alert(error.message);<br>      }</strong><br>);</pre><p>subscribe() ရဲ့ second param အနေနဲ့ error ကိုလက်ခံလိုက်ပြီး handle လုပ်လိုက်တာ တွေ့ရမှာပါ။</p><p>ပြန်စမ်းကြည့်ရင်တော့ count 3 ကျော်သွားရင် အခုလို တွေ့ရမှာပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PEu8FA7yflEuEinePnuYBQ.png" /><figcaption>Observable error</figcaption></figure><p>Completion စမ်းဖို့အတွက် setInterval() အပေါ်နားလေးမှာ အခုလို ရေးလိုက်ပါ။</p><pre>setInterval(() =&gt; {<br>    if (count == 2) <strong>observer.complete()</strong>;<br>...</pre><p>count 2 ဖြစ်ရင် ဆက် emit မလုပ်တော့ဖို့အတွက် complete() ခေါ်လိုက်တာဖြစ်ပါတယ်။ complete ဖြစ်သွားရင် observable လုံးဝ ရပ်သွားမှာ ဖြစ်ပါတယ်။ အကယ်၍ complete မခေါ်ခင် error တက်သွားရင် complete ကိုဆက်မခေါ်ပေးပါ။</p><p>complete ဖြစ်သွားပြီးမှ လုပ်စေချင်တဲ့ code တွေရှိရင်အခုလို handle လုပ်နိုင်ပါတယ်။ ngOnInit() ထဲမှာအခုလိုရေးပါ။</p><pre>this.firstSub = this.customInterval().subscribe(<br>      (count) =&gt; {<br>        console.log(count);<br>      },<br>      (error) =&gt; {<br>        alert(error.message);<br>      },<br>      <strong>() =&gt; {<br>        console.log(&#39;Completed!&#39;);<br>      }</strong><br> );</pre><p>subscribe() ရဲ့ third param အနေနဲ့ complete ကိုလက်ခံရရှိပြီး handle လုပ်လိုက်ပါတယ်။ complete က data မပါတဲ့အတွက် no argument အနေနဲ့ ရေးထားတာကို တွေ့ရမှာပါ။</p><p>ပြန်စမ်းကြည့်ရင်တော့ ဒီလိုတွေ့ရမှာပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/572/1*czSbQjnmzlJv-1-QUlyz_w.png" /><figcaption>Observable completion</figcaption></figure><p>ဒီနည်းတွေနဲ့ Observable တစ်ခုကို လိုအပ်သလို emit လုပ်နိုင် handle လုပ်နိုင်မှာ ဖြစ်ပါတယ်။</p><p><strong>Previous: </strong><a href="https://medium.com/learn-ng/observable-66fd5ab0d438"><strong>Observable</strong></a></p><p><strong>Next: </strong><a href="https://medium.com/learn-ng/operators-56e0f0f2700a"><strong>Operators</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=853ffb42787c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/learn-ng/working-with-custom-observable-853ffb42787c">Working with Custom Observable</a> was originally published in <a href="https://medium.com/learn-ng">Learn Ng</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Observable]]></title>
            <link>https://medium.com/learn-ng/observable-66fd5ab0d438?source=rss----23699d95b94c---4</link>
            <guid isPermaLink="false">https://medium.com/p/66fd5ab0d438</guid>
            <category><![CDATA[learn-angular]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Ye Min Ko]]></dc:creator>
            <pubDate>Wed, 01 Sep 2021 06:24:58 GMT</pubDate>
            <atom:updated>2021-09-01T10:37:21.597Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*W2sI--Vk_5VkqFzUmRbiCg.png" /><figcaption>RxJS logo</figcaption></figure><h3>What is an Observable?</h3><p>Observable ဆိုတာ data source တစ်ခုပါပဲ၊ ဥပမာအားဖြင့် user ဆီက event တွေ Http requests တွေနဲ့ ကိုယ်တိုင်ရေးသားထားတဲ့ code တွေကနေ ရရှိလာတဲ့ data တွေကို တစ်ကြိမ်ထပ်မက ထုတ်လွှင့်ပေးနိုင်တဲ့ စနစ်တစ်ခုပဲ ဖြစ်ပါတယ်။</p><p>ထုတ်လွှင့်လိုက်တဲ့ data တွေကို လက်ခံရယူလိုသူ (Observer) တွေက subscribe လုပ်ထားချင်းဖြင့် ရယူနိုင်ပါတယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kYRIIHzM7ALTzA6UCklMcg.png" /><figcaption>What is an Observable?</figcaption></figure><p>Observable ကနေ data တွေအပြင် error နဲ့ ထုတ်လွှင့်မှုပြီးဆုံးကြောင်း (completion) တွေပါ ထုတ်လွှင့်နိုင်တာကြောင့် subscribe လုပ်ထားသူတွေက ရရှိလာတဲ့ ထုတ်လွှင့်မှုအပေါ်မူတည်ပြီး လိုသလို အသုံးချနိုင်မှာ ဖြစ်ပါတယ်။</p><p>Angular က RxJS ရဲ့ Observer pattern ကိုအသုံးပြုထားတာကြောင့် အသေးစိတ်လေ့လာလိုပါက <a href="https://rxjs.dev/"><strong>ဒီမှာ</strong></a> လေ့လာနိုင်ပါတယ်။</p><h3>Using Observable</h3><p>Observable တချို့ကိုရှေ့မှာ အသုံးပြုခဲ့ကြပါတယ်။ ဥပမာအားဖြင့် route ရဲ့ params ပါ။ အောက်ပါ code ကိုကြည့်ရင် မှတ်မိမှာပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I9nc0OD8LxiMbTWBsKwrVg.png" /><figcaption>Subscribe to route params</figcaption></figure><p>လက်တွေ့မြင်နိုင်စေဖို့အတွက် Observable တစ်ခုကို အခုလို အသုံးပြုကြပါမယ်။ HomeComponent ကိုဖွင့်ပြီး အခုလိုရေးပါ။</p><pre>import { <strong>interval </strong>} from &#39;rxjs&#39;;</pre><pre>ngOnInit() {<br>    <strong>interval</strong>(1000).subscribe((count: number) =&gt; {<br>      console.log(count);<br>    });<br>}</pre><p>interval ဆိုတာ Observable တစ်ခုဖြစ်ပါတယ်။ rxjs ကနေ import လုပ်ပေးဖို့လိုအပ်ပါတယ်။ interval ကို subscribe လုပ်လိုက်ပြီးရလာတဲ့ data ကို log ထုတ်ထားပါတယ်။</p><p>အားလုံးပြီးရင်တော့ run လိုက်ပါ။ home tab ကိုနှိပ်လိုက်ရင် console မှာ count တွေထွက်လာတာ တွေ့ရမှာပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E4oJn1TfHWpatD6e1lG03g.png" /><figcaption>Home tab</figcaption></figure><p>Home tab ကနေတခြား tab တစ်ခုကို နှိပ်ကြည့်ပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Oeamug4w-ttbE2Txidd8PA.png" /><figcaption>Servers tab</figcaption></figure><p>count တွေဆက်ပြီးသွားနေတာ တွေ့ရမှာပါ။ ဘာဖြစ်လို့လဲဆိုတော့ observable တွေကို subscribe လုပ်လိုက်ရင် component မရှိတော့လည်း ဆက်ပြီးတော့ subscribe လုပ်ပေးနေလို့ပဲဖြစ်ပါတယ်။</p><p>ဒါကြောင့် component မရှိတော့တဲ့အခါ subscribe လုပ်ထားတာကိုရပ်ဖို့ အခုလိုပြင်ရေးပါ။</p><pre>import { interval, <strong>Subscription </strong>} from &#39;rxjs&#39;;</pre><pre>firstSub: <strong>Subscription</strong> = new <strong>Subscription</strong>();</pre><pre>ngOnInit(): void {<br>    this.<strong>firstSub </strong>= interval(1000).subscribe((count: number) =&gt; {<br>      console.log(count);<br>    });<br>  }</pre><pre>ngOnDestroy() {<br>    this.<strong>firstSub</strong>.<strong>unsubscribe</strong>();<br>}</pre><p>Component destroy လုပ်ရင် subscribe လုပ်ထားတာကို unsubscribe လုပ်ပေးဖို့ပြောထားတာပါ။ ဒါဆိုရင်ပြန်စမ်းကြည့်ပါ။ Home tab ကနေတခြား tab ကိုရောက်သွားတဲ့အခါ count ထုတ်ရပ်သွားတာတွေ့ရမှာ ဖြစ်ပါတယ်။</p><p>ဒါကြောင့် observable တွေကို အသုံးပြုမယ်ဆိုရင် အမြဲတမ်း unsubscribe လုပ်ပေးဖို့လိုအပ်မှာ ဖြစ်ပါတယ်။</p><p>ဒါပေမဲ့ Angular က အသုံးပြုထားတဲ့ observable တွေ ဥပမာ params တို့ httpClient တို့ကတော့ Angular ကအလိုလျှောက် unsubscribe လုပ်ပေးတာဖြစ်လို့ ကိုယ်တိုင်လုပ်ပေးဖို့ မလိုပါဘူး။</p><p><strong>Previous: </strong><a href="https://medium.com/learn-ng/more-in-routing-ce9b349ca831"><strong>More in Routing</strong></a></p><p><strong>Next: </strong><a href="https://medium.com/learn-ng/working-with-custom-observable-853ffb42787c"><strong>Working with Custom Observable</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=66fd5ab0d438" width="1" height="1" alt=""><hr><p><a href="https://medium.com/learn-ng/observable-66fd5ab0d438">Observable</a> was originally published in <a href="https://medium.com/learn-ng">Learn Ng</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript — Primitive Vs Reference]]></title>
            <link>https://medium.com/learn-ng/javascript-primitive-vs-reference-a475c95a1b31?source=rss----23699d95b94c---4</link>
            <guid isPermaLink="false">https://medium.com/p/a475c95a1b31</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[js]]></category>
            <category><![CDATA[datatypes-in-javascript]]></category>
            <category><![CDATA[primitive-vs-reference]]></category>
            <dc:creator><![CDATA[Ye Min Ko]]></dc:creator>
            <pubDate>Mon, 30 Aug 2021 18:13:56 GMT</pubDate>
            <atom:updated>2021-08-30T18:13:56.405Z</atom:updated>
            <content:encoded><![CDATA[<h3>JavaScript — Primitive Vs Reference</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*07hx56EQoR0GHcfW" /><figcaption>Photo by <a href="https://unsplash.com/@lazycreekimages?utm_source=medium&amp;utm_medium=referral">Michael Dziedzic</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>JavaScript နဲ့ code တွေရေးတဲ့အခါ တခါတလေ data type နဲ့ပတ်သက်ပြီး ကိုယ်ထင်ထားတဲ့အတိုင်း ဖြစ်မလာတာတွေ ဒါမှမဟုတ် ဘာဖြစ်နေမှန်းမသိ လိုချင်တဲ့အဖြေရမလာတာမျိုးတွေ ရှိနိုင်ပါတယ်။</p><p>ဒါကြောင့် ဒီ article မှာ JavaScript ရဲ့ primitive type နဲ့ reference type တွေမတူညီပုံ၊ memory မှာနေရာယူပုံတို့ကို နားလည်လွယ်အောင် ရှင်းပြမှာဖြစ်ပါတယ်။</p><p><strong>Primitive Type</strong> တွေကတော့ boolean, number, string, undefined, null, symbol(ES6) တို့ဖြစ်ပါတယ်။</p><p><strong>Reference Type </strong>တွေကတော့ Object, Array နဲ့ Function တို့ဖြစ်ပါတယ်။</p><h3>Primitive Types</h3><p>ပထမဆုံး primitive type တွေအလုပ်လုပ်ပုံကို ကြည့်ကြပါမယ်။ အောက်ပါ code ကိုကြည့်ပါ။</p><pre>let a = &quot;Hello&quot;;</pre><p>ဒီလိုရေးလိုက်ရင် memory မှာ အခုလို နေရာယူသွားမှာ ဖြစ်ပါတယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xi13YCY8ulMNg6Yz5Mmqew.png" /><figcaption>Primitive Types — 1</figcaption></figure><p>Value တွေထားဖို့ memory ပေါ်မှာ Stack နဲ့ Heap နှစ်ခုရှိတဲ့ထဲကမှ primitive type တွေကတော့ Stack ပေါ်မှာ နေရာယူပါတယ်။</p><p>အောက်ပါ code ကို ထပ်မံရေးလိုက်မယ် ဆိုရင်တော့…</p><pre>let b = a;</pre><p>b ထဲ a ကို assign လုပ်လိုက်ပါတယ်။ ဒါကြောင့် a ထဲက “Hello” value ကို b ထဲထည့်ပေးလိုက်ပြီး memory မှာ အခုလို နေရာယူသွားမှာ ဖြစ်ပါတယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XReT-iykyH-vVfFWGzoNRw.png" /><figcaption>Primitive Types — 2</figcaption></figure><p>အကယ်၍ a ရဲ့ value ကို ဒီလိုပြောင်းလိုက်မယ်ဆိုရင် b ရဲ့ value က ဘာဖြစ်သွားမလဲ?</p><pre>a = &quot;Hi&quot;;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3P_mE3Az2QbUt7iw8ChCww.png" /><figcaption>Primitive Types — 3</figcaption></figure><p>a ရဲ့ value ပြောင်းလိုက်တာကြောင့် Stack ပေါ်မှာ နောက်ထပ်နေရာ တစ်ခုထပ်ယူပြီး value အသစ်ထည့်လိုက်တာ တွေ့ရမှာပါ။ နဂို value အဟောင်းကတော့ point လုပ်တော့မှာ မဟုတ်တဲ့အတွက် ပျက်ပြယ်သွားမှာ ဖြစ်ပါတယ်။ b ကတော့ နဂိုအတိုင်းသာဆက်ရှိနေတာကြောင့် b ရဲ့ value သည် “Hello” ပဲ ဆက်ဖြစ်နေမှာ ဖြစ်ပါတယ်။</p><p>ဒီအတိုင်းပဲ b value ပြောင်းရင်လည်း အခုလို နေရာယူသွားမှာ ဖြစ်ပါတယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j1ALH_L_AJd0IMj3qUbKgA.png" /><figcaption>Primitive Types — 4</figcaption></figure><p>တခြား primitive type တွေလည်း ဒီလိုပုံစံနဲ့ပဲ အလုပ်လုပ်ပါတယ်။</p><h3>Objects</h3><p>Reference type တစ်ခုဖြစ်တဲ့ object အလုပ်လုပ်ပုံကတော့ primitive type တွေနဲ့ကွဲပြားပါတယ်။ အောက်ပါ code ကိုကြည့်ပါ။</p><pre>let a = { name: &quot;Mg Mg&quot;};</pre><p>Memory မှာအခုလို နေရာယူပေးပါတယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fhbP888aWhiEMCXTozjLMQ.png" /><figcaption>Objects — 1</figcaption></figure><p>Object တစ်ခုကို a ထဲထည့်တဲ့အခါ Stack ပေါ်မှာ Object ရဲ့ တည်နေရာဖြစ်တဲ့ memory address ကိုသိမ်းလိုက်ပါတယ်။ Object တွေက Heap ပေါ်မှာ ထားရတာဖြစ်လို့ ပုံပါအတိုင်း Heap ထဲမှာ သိမ်းလိုက်ပါတယ်။</p><p>ဒီနေရာမှာ သတိထားရမှာကတော့ primitive တုန်းက stack ပေါ်မှာ value ကိုတိုက်ရိုက်သိမ်းပေမဲ့ ဒီမှာကတော့ value အဖြစ် obj ရဲ့ address ကိုသာသိမ်းတာ ဖြစ်ပါတယ်။</p><p>ဆက်လက်ပြီး အခုလိုရေးမယ်ဆိုရင်တော့…</p><pre>let b = a;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nGYC_4jPkWX8V8H1yKi6Gw.png" /><figcaption>Objects — 2</figcaption></figure><p>b ထဲ a ကို assign လုပ်တဲ့အခါ a ရဲ့ value ဖြစ်တဲ့ address ကိုသာပေးလိုက်ပါတယ်။ Obj ပေးတာမဟုတ်ပါဘူး။</p><p>ဒါကြောင့် b က Stack ပေါ်မှာနေရာတစ်ခုယူပြီး obj ရဲ့ address ကိုမှတ်လိုက်ပါတယ်။ မှတ်လိုက်တဲ့ address က a နဲ့အတူတူပဲဖြစ်တာကြောင့် Heap ထဲက a ညွှန်ထားတဲ့ obj ကိုပဲ လှမ်းညွှန်လိုက်မှာ ဖြစ်ပါတယ်။</p><p>အကယ်၍ a ကအခုလို ပြောင်းလဲလိုက်ရင် b ပါလိုက်ပြောင်းမလား?</p><pre>a.name = &quot;Su Su&quot;;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4XFR838hHV09eNau0hslfA.png" /><figcaption>Objects — 3</figcaption></figure><p>Heap ထဲက obj ရဲ့ name property ကိုပြောင်းလိုက်တာဖြစ်ပါတယ်။ b ကလည်း အဲ့ဒီ obj ကိုပဲထောက်ထားတာ ဖြစ်တဲ့အတွက် b ပါ လိုက်ပြောင်းသွားမှာ ဖြစ်ပါတယ်။</p><p>ဒါဆိုရင် ဒီလိုရေးရင်ရော b လိုက်ပြောင်းမှာလား?</p><pre>a = { name: &quot;Htoo&quot; };</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LWMep1T9DBJlgYRpCFfMEg.png" /><figcaption>Object s— 4</figcaption></figure><p>Obj အသစ်တစ်ခုကို a ထဲ assign လုပ်လိုက်တာကြောင့် Heap ပေါ်မှာ Obj ကိုသိမ်းပြီး ထို obj ရဲ့ address ကို Stack ပေါ်မှာသိမ်းလိုက်တာတွေ့ရမှာပါ။ အရင် address အဟောင်းကတော့ ပျက်ပြယ်သွားမှာဖြစ်ပါတယ်။ ပုံအရဆိုရင် a နဲ့ b ကသီးခြားစီဖြစ်သွားတဲ့အတွက် a ရဲ့ပြောင်းလဲမှုကို b ကသိတော့မှာမဟုတ်ပါ။</p><p>ဒီ သဘောတရားက a မှမဟုတ်ပါဘူး b ကို b = { name: “Htoo”} ရေးရင်လည်းဒီလိုပဲ အလုပ်လုပ်ပေးမှာ ဖြစ်ပါတယ်။ ကိုယ်တိုင်စမ်းသပ်ကြည့်ပါ။</p><h3>Objects with Arrays</h3><p>Object ထဲမှာ array တွေနဲ့ တခြားသော object တွေပါတတ်ပါတယ်။ ဒီလိုပါလာခဲ့ရင် ဘယ်လိုအလုပ်လုပ်လဲ ဆိုတာ ဆက်ကြည့်ရအောင်ပါ။</p><p>အောက်ပါ code ကိုကြည့်ပါ။</p><pre>let a = { <br>      name: &quot;Mg Mg&quot;,<br>      hobbies: [&quot;Sports&quot;]<br>    }</pre><pre>let b = a;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-_k9wfR-UNYxcibue5Nvag.png" /><figcaption>Objects with Arrays — 1</figcaption></figure><p>Obj ထဲမှာ array တစ်ခုပါလာတဲ့အတွက် Heap ပေါ်မှာ array ကိုသိမ်းလိုက်ပြီး အဲ့ array ရဲ့ address ကိုတော့ obj ထဲမှာ မှတ်ထားလိုက်တာကို တွေ့ရမှာပါ။ ကျန်တာကတော့ သိပြီးသားတွေပါ။</p><p>a ကအခုလိုပြောင်းလဲလိုက်ရင် b ပါလိုက်ပြောင်းလဲမလား?</p><pre>a.hobbies = [&quot;Cooking&quot;];</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7iLNXsAee_MUdSaUlpfDFg.png" /><figcaption>Objects with Arrays — 2</figcaption></figure><p>array ကိုပဲပြောင်းလိုက်တာမလို့ Heap ပေါ်မှာအသစ်ထပ်သိမ်းလိုက်တဲ့ address ကိုပြောင်းမှတ်လိုက်ပါတယ်။ b ကကြည့်မယ်ဆိုရင် ပြောင်းလဲသွားတဲ့ address ကိုမြင်နေရတာမလို့ b ပါ လိုက်ပြောင်းသွားမှာ ဖြစ်ပါတယ်။</p><p>Array အပြင် object တွေပါလာခဲ့ရင်လည်း ဒီလိုပဲအလုပ်လုပ်ပေးမှာ ဖြစ်ပါတယ်။</p><h3>Arrays</h3><p>Array တွေရဲ့အလုပ်လုပ်ပုံကို ဆက်လေ့လာရအောင်ပါ။ Array ကလည်း object type ပဲဖြစ်တဲ့အတွက် အလုပ်လုပ်ပုံတွေ အတူတူပဲဖြစ်နေမှာပါ။</p><pre>let a = [1, 2, 3];</pre><p>ဒီလိုရေးရင် memory မှာ အခုလို a နေရာယူပေးပါတယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SczOGmmKBVbRe-OMRpwwSQ.png" /><figcaption>Arrays — 1</figcaption></figure><p>Object တုံကလိုပဲ Heap ပေါ်မှာ array ကိုသိမ်းပြီး address ကိုတော့ stack မှာမှတ်လိုက်ပါတယ်။ ဆက်လက်ပြီး …</p><pre>let b = a;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hx43j65Drm9zvCc_YPpcvA.png" /><figcaption>Arrays — 2</figcaption></figure><p>b က a ရဲ့ address ကိုရယူလိုက်ပြီး တူညီတဲ့ array ကိုပဲ point လုပ်လိုက်ပါတယ်။</p><p>အခုလိုရေးရင် b ကပြောင်းလဲမှာလား?</p><pre>a.push(4);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IpqV3SHNFtXZawYxSjybGg.png" /><figcaption>Arrays — 3</figcaption></figure><p>push() လုပ်လိုက်တာကြောင့် Heap ထဲက array မှာသွားပြင်လိုက်ပါတယ်။ b ကလည်း ဒီ array ကိုပဲ point လုပ်ထားတာမလို့ b ပါ လိုက်ပြောင်းသွားမှာ ဖြစ်ပါတယ်။</p><p>ဒီလိုရေးရင်ရော b လိုက်ပြောင်းမှာလား?</p><pre>a = [100];</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Jq3lSoid0o8_QXV9izFm3w.png" /><figcaption>Arrays — 4</figcaption></figure><p>a ထဲကို array အသစ်ထည့်လိုက်တာဖြစ်လို့ Heap မှာ ထို array အသစ်ကိုသိမ်းလိုက်ပါတယ်။ ပြီးတော့ stack ပေါ်မှာ ထို array ရဲ့ address ကိုမှတ်လိုက်ပါတယ်။ နဂို array ရဲ့ address ကတော့အသုံးမပြုတော့လို့ ပျက်ပြယ်သွားပါတယ်။ ပုံကိုကြည့်မယ်ဆိုရင် a နဲ့ b က သီးခြားစီဖြစ်သွားလို့ b က a ရဲ့ပြောင်းလဲခြင်းတွေကို မသိတော့ပါ။</p><p>ဒီ သဘောတရားက b = [100] ရေးရင်လည်းအတူတူပဲဖြစ်မှာဖြစ်ပါတယ်။ ကိုယ်တိုင် လက်တွေ့ရေးသားကြည့်ပါ။</p><h3>Arrays with Objects</h3><p>Array ထဲမှာလည်း obj တွေ တခြား array တွေပါနိုင်ပါတယ်။ အောက်ပါ code ကိုကြည့်ပါ။</p><pre>let a = [ 1, 2, { name: &quot;Mg Mg&quot;} ];<br>let b = a;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KdxLfS8YfG9kMl6vwcO6Fg.png" /><figcaption>Arrays with Objects — 1</figcaption></figure><p>Array ထဲမှာ obj တစ်ခုပါလာတာဖြစ်လို့ Heap မှာ obj ကိုသိမ်းလိုက်ပြီး obj ရဲ့ address ကို array ထဲမှာ မှတ်လိုက်တာတွေ့ရမှာပါ။ ကျန်တာကတော့ သိပြီးသားတွေပဲဖြစ်ပါတယ်။</p><p>ဒီလိုရေးလိုက်ရင် b ပြောင်းလဲမှာလား?</p><pre>a[2] = { age: 12 };</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v52XDTR9eRvO1hvLzeX7Lg.png" /><figcaption>Arrays with Objects — 2</figcaption></figure><p>Array ထဲက obj ကိုပြင်လိုက်တဲ့အတွက် Heap မှာ obj အသစ်ကိုသိမ်းလိုက်ပြီး ထို obj ရဲ့ address ကို array မှာ update လုပ်လိုက်တာ တွေ့ရမှာပါ။ b ကနေကြည့်ရင် array ထဲက address ပြောင်းသွားတာမြင်ရတာဖြစ်လို့ b ပါ လိုက်ပြောင်းသွားမှာ ဖြစ်ပါတယ်။</p><p>ဒီနည်းအတိုင်းပဲ array ထဲမှာ array တွေထပ်ပါလာခဲ့ရင်လည်း ဒီလိုပဲအလုပ်လုပ်မှာဖြစ်ပါတယ်။</p><p>အခုဆိုရင် ဘယ်လိုရေးသားရင်တော့ reference လုပ်ထားတဲ့ variable မှာပြောင်းလဲမှုတွေဖြစ်ပြီး ဘယ်လိုရေးရင် မပြောင်းဘူးလဲဆိုတာ သိလောက်မယ် ထင်ပါတယ်။ လက်တွေ့စမ်းကြည့်ပြီး သေချာနားလည်အောင်လုပ်ပါ။</p><h3>Shadow Copy</h3><p>တချို့အခြေအနေတွေမှာ reference type တွေကို ပြောင်းလဲမှုတွေလုပ်တဲ့အခါ original variable ကိုမထိခိုက်စေချင်ရင် clone လုပ်ပြီးအသုံးပြုကြပါတယ်။ ဒီလိုလုပ်တဲ့အခါမှာ အောက်ပါနည်းတွေကိုသုံးရင် Shadow Copy ဖြစ်စေပါတယ်။</p><pre>let a = { name: &quot;Mg Mg&quot;, address: { street: &quot;1st street&quot;}}};<br>let b = {...a};<br>let c = Object.assign({}, a);</pre><p>ဒီလို clone လုပ်ရင် memory မှာအခုလိုနေရာယူပေးပါတယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MORqI3mavyVESL6xm_C7Qw.png" /><figcaption>Shadow Copy</figcaption></figure><p>Shadow Copy လုပ်လိုက်ရင် Obj ထဲက primitive type တွေကိုပဲ clone လုပ်ပေးတာဖြစ်ပြီး reference type တွေကိုတော့ နဂိုအတိုင်းညွှန်ပေးထားတာဖြစ်လို့ အကယ်၍ reference type ထဲက data ကိုပြင်မိရင် original variable ထဲမှာပါသွားပြီးသက်ရောက်စေမှာ ဖြစ်ပါတယ်။</p><h3>Deep Copy</h3><p>အကယ်၍ ကိုယ်က reference type တွေကိုပါ clone လုပ်စေချင်ရင် Deep Copy ဖြစ်အောင် အောက်ပါနည်းလမ်းကို သုံးနိုင်ပါတယ်။</p><pre>let a = { name: &quot;Mg Mg&quot;, address: { street: &quot;1st street&quot;}}};<br>let b = JSON.parse(JSON.stringify(a));</pre><p>ဒီလိုရေးရင်တော့ memory မှာ အခုလို ဖြစ်သွားမှာပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q7CDN4W-thdTGTMy4YbpfQ.png" /><figcaption>Deep Copy</figcaption></figure><p>Obj ရဲ့ primitive type တွေကိုပဲ clone လုပ်ပေးတာမဟုတ်တော့ပဲ obj ထဲကတခြား obj တွေနဲ့ array တွေကိုပါ သီးခြားထုတ်ပြီး clone လုပ်ပေးလိုက်ပါတယ်။ ဒါကြောင့် original variable ကို သက်ရောက်မှာ မဟုတ်တော့ပါ။</p><p>ဒီနည်းအတိုင်းပဲ Array တွေကို clone လုပ်ရင်လည်း အတူတူပဲဖြစ်ပါတယ်။</p><blockquote>အခုဖော်ပြခဲ့တဲ့ Shadow Copy နဲ့ Deep Copy ပြုလုပ်နည်းတွေက သဘောတရားနားလည်အောင်သာ ဥပမာပေးတာ ဖြစ်ပြီး ဒီနည်းတွေပဲ အသုံးပြုလို့ရတာ မဟုတ်ပါ။</blockquote><p>ဒါဆိုရင်တော့ အနည်းဆုံး data types တွေရဲ့ အလုပ်လုပ်ပုံကို သဘောပေါက်မယ်လို့ယူဆပါတယ်။ ပိုမိုသိရှိဖို့အတွက် အောက်ပါ References တွေကို ဆက်လက်လေ့လာပေးပါ။</p><h3>References</h3><ul><li><a href="https://academind.com/tutorials/reference-vs-primitive-values/">Reference vs Primitive Values</a></li><li><a href="https://www.javascripttutorial.net/javascript-primitive-vs-reference-values/">JavaScript Primitive vs. Reference Values</a></li><li><a href="https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language?page=1&amp;tab=votes#tab-top">Is JavaScript a pass-by-reference or pass-by-value language?</a></li><li><a href="https://codeburst.io/explaining-value-vs-reference-in-javascript-647a975e12a0">Explaining Value vs. Reference in Javascript</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a475c95a1b31" width="1" height="1" alt=""><hr><p><a href="https://medium.com/learn-ng/javascript-primitive-vs-reference-a475c95a1b31">JavaScript — Primitive Vs Reference</a> was originally published in <a href="https://medium.com/learn-ng">Learn Ng</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[More in Routing]]></title>
            <link>https://medium.com/learn-ng/more-in-routing-ce9b349ca831?source=rss----23699d95b94c---4</link>
            <guid isPermaLink="false">https://medium.com/p/ce9b349ca831</guid>
            <category><![CDATA[learn-angular]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Ye Min Ko]]></dc:creator>
            <pubDate>Wed, 23 Jun 2021 10:37:59 GMT</pubDate>
            <atom:updated>2021-09-01T06:25:40.789Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*RJyv-YGEB8I4I0AY" /><figcaption>Photo by <a href="https://unsplash.com/@weirick?utm_source=medium&amp;utm_medium=referral">Jake Weirick</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Routing နဲ့ပတ်သက်ပြီး အတော်များများကို လေ့လာပြီးတဲ့နောက်မှာ သိသင့်သိထိုက်တဲ့တချို့ feature တွေကိုဆက်ပြီး လေ့လာရပါမယ်။</p><h3>canDeactivate</h3><p>Route တစ်ခုကနေ တစ်ခြား route ကိုကူးတဲ့အခါ လက်ရှိ route ကနေ ထွက်ခွာခွင့် ရှိမရှိ စစ်ဖို့အတွက် အသုံးပြုပါတယ်။ များသောအဖြင့် တစ်ခုခု edit လုပ်ပြီး မsaveပဲ တစ်ခြား route ကူးသွားခြင်းကို ကာကွယ်ဖို့အတွက် သုံးကြပါတယ်။</p><p>လက်တွေ့စမ်းသပ်ဖို့အတွက် ServerEditComponent မှာ အခုလိုရေးပါ။</p><pre>export class ServerEditComponent implements OnInit {</pre><pre> changesSaved = false;</pre><pre> constructor(<br>    private route: ActivatedRoute,<br>    private serverService: ServerService,<br>    private router: Router<br>  ) {}</pre><pre> onUpdate() {<br>    this.serverService.updateServer(this.server);<br>    this.changesSaved = true;<br>    this.router.navigate([&#39;../&#39;], { relativeTo: this.route });<br>  }</pre><pre> ngOnInit() {<br>    this.route.params.subscribe((params: Params) =&gt; {<br>      this.server = this.serverService.getServer(+params[&#39;id&#39;]);<br>    });<br> }<br>}</pre><p>Update လုပ်ဖို့ service နဲ့ ချိတ်ဆက်လိုက်တာ ဖြစ်ပါတယ်။ Update လုပ်ပြီးရင် အပေါ်အဆင့် route ကို navigate လုပ်သွားမှာပါ။</p><p>ဆက်လက်ပြီးတော့ update မနှိပ်ပဲ တခြား route ပြောင်းသွားမှာကို ကာကွယ်ဖို့ဆက်ရေးပါမယ်။</p><p>CanDeactivateGuardService ကို generate လုပ်ပါ။ အထဲမှာအောက်ပါ code တွေရေးပါ။</p><pre>export interface CanComponentDeactivate {<br>  canDeactivate: () =&gt; Observable&lt;boolean&gt; | Promise&lt;boolean&gt; | boolean;<br>}</pre><pre>@Injectable({<br>  providedIn: &#39;root&#39;,<br>})<br>export class CanDeactivateGuardService<br>  implements <strong>CanDeactivate</strong>&lt;CanComponentDeactivate&gt;<br>{<br>  canDeactivate(<br>    component: CanComponentDeactivate,<br>    currentRoute: ActivatedRouteSnapshot,<br>    currentState: RouterStateSnapshot,<br>    nextState?: RouterStateSnapshot<br>  ): Observable&lt;boolean&gt; | Promise&lt;boolean&gt; | boolean {<br>    return component.canDeactivate();<br>  }<br>}</pre><p>CanDeactivate အတွက် interface တစ်ခုကို ထည့်ပေးထားပါတယ်။ canDeactivate() ထဲမှာ ခေါ်သုံးမဲ့ component က override လုပ်မဲ့ canDeactivate() ကိုထပ်ဆင့် ခေါ်ပေးထားပါတယ်။</p><p>ပြီးရင်တော့ AppRoutingModule မှာအခုလိုရေးပါ။</p><pre>const appRoutes: Routes = [<br>...<br>{<br>    path: &#39;servers&#39;,<br>    ...<br>    children: [<br>      ...<br>      {<br>        path: &#39;:id/edit&#39;,<br>        component: ServerEditComponent,<br>        <strong>canDeactivate</strong>: [<strong>CanDeactivateGuardService</strong>],<br>      },<br>    ],<br>  },<br>]</pre><p>ServerEditComponent မှာအခုလိုရေးပါ။</p><pre>export class ServerEditComponent implements OnInit, <strong>CanComponentDeactivate</strong> {</pre><pre> <strong>canDeactivate</strong>(): Observable&lt;boolean&gt; | Promise&lt;boolean&gt; | boolean {<br>    if (!this.allowEdit) return true;<br>    return this.changesSaved<br>      ? true<br>      : confirm(&#39;Do you want to discard the changes?&#39;);<br>  }<br>}</pre><p>canDeactivate() ထဲမှာ deactivate လုပ်ခွင့်ရှိရင် true ပြန်ပေးပြီး မရှိရင် false ပြန်ပေးလိုက်တာ ဖြစ်ပါတယ်။</p><p>အားလုံးပြီးရင် အခုလိုစမ်းကြည့်ပါ။ Edit ထဲရောက်ပြီး update မနှိပ်ပဲတခြား route ကို သွားမယ်ဆိုရင် confirm box တက်လာမှာ ဖြစ်ပါတယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*KSTikZdtrbwbYeB6c_nJug.gif" /><figcaption>canDeactivate Guard</figcaption></figure><h3>Static Data</h3><p>Route မှာ static data တချို့ ထည့်ပေးချင်ရင် သုံးပါတယ်။</p><p>PageNotFoundComponent မှာ အခုလိုရေးပါ။</p><pre><strong><em>HTML</em></strong><br>&lt;h3&gt;{{ errorMessage }}&lt;/h3&gt;</pre><pre><strong><em>TypeScript</em></strong><br>errorMessage: string = &#39;&#39;;<br>constructor(private route: ActivatedRoute) {}<br>ngOnInit(): void {<br>    this.errorMessage = this.route.snapshot.<strong>data</strong>[&#39;message&#39;];<br>}</pre><p>params, queryParams တို့လိုပဲ အခုလို snapshot နဲ့ရယူနိုင်သလို၊ subscribe လုပ်ပြီးလည်းရယူနိုင်ပါတယ်။</p><p>AppRoutingModule ရဲ့ wildcard မှာ အခုလိုပြင်လိုက်ပါ။</p><pre>{<br>    path: &#39;**&#39;,<br>    component: PageNotFoundComponent,<br>    <strong>data</strong>: { message: &#39;Page Not Found&#39; },<br>},</pre><p>data property က js obj တစ်ခုကို လက်ခံပါတယ်။ ဒါကြောင့် ကိုယ်ထည့်ပေးချင်တဲ့ data ကို အခုလို ထည့်ပေးလိုက်တာပါ။</p><p>ပြီးရင်တော့ URL မှာ မသတ်မှတ်ထားတဲ့ route ကိုရိုက်ထည့်ရင် အခုလိုတွေ့ရမှာ ဖြစ်ပါတယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N-tdqRj5T50x4snTh6BFPw.png" /><figcaption>Page Not Found</figcaption></figure><h3>Dynamic Data (or) Resolver</h3><p>Route အတွက် data ကို dynamic ရယူချင်ရင် အသုံးပြုပါတယ်။ Route ကိုမရောက်ခင် data ကို အရင်ယူပေးပါတယ်။</p><p>ServerComponent folder ထဲမှာ ServerResolverService ကိုအခုလို generate လုပ်လိုက်ပါ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/470/1*fTNaylKMJ89pbWOnC3oU2Q.png" /><figcaption>server-resolver.service.ts</figcaption></figure><p>ပြီးရင်တော့အောက်ပါ code ကိုရေးပါ။</p><pre>@Injectable({<br>  providedIn: &#39;root&#39;,<br>})<br>export class ServerResolverService implements <strong>Resolve</strong>&lt;Server&gt; {<br>  constructor(private serverService: ServerService) {}</pre><pre><strong>resolve</strong>(<br>    route: ActivatedRouteSnapshot,<br>    state: RouterStateSnapshot<br>  ): Observable&lt;Server&gt; | Promise&lt;Server&gt; | Server {<br>    return this.serverService.<strong>getServer</strong>(+route.params[&#39;id&#39;]);<br>  }<br>}</pre><p>Resolve ကို implement လုပ်ရပါတယ်။ resolve() method ကို override လုပ်ပြီး ရယူလိုတဲ့ data ကို return ပြန်ပေးရပါတယ်။</p><p>AppRoutingModule ရဲ့ ServerComponent route မှာအခုလိုရေးပါ။</p><pre>{<br>      path: &#39;:id&#39;,<br>      component: ServerComponent,<br>      <strong>resolve</strong>: { <strong>server</strong>: ServerResolverService },<br>},</pre><p>resolve property က js obj ကိုလက်ခံပါတယ်။ Key ကို server လို့ နံမည်ပေးလိုက်ပြီး ServerResolverServiceကို ထည့်ပေးလိုက်ပါတယ်။</p><p>ServerComponent မှာတော့ အခုလိုပြင်လိုက်ပါ။</p><pre>ngOnInit() {<br>   // this.route.params.subscribe((params: Params) =&gt; {<br>   //   this.server = this.serverService.getServer(+params[&#39;id&#39;]);<br>   // });</pre><pre>this.route.<strong>data</strong>.subscribe((data: Data) =&gt; {<br>      this.server = data[&#39;server&#39;];<br>    });<br>}</pre><p>Static data ရယူတုန်းကလိုပဲ dynamic data ကိုရယူရပါတယ်။ ဒီနေရာမှာတော့ subscribe လုပ်ပြီး ရယူထားပါတယ်။</p><p>ပြန်စမ်းကြည့်ပါ server တွေ အရင်အတိုင်းဆက်ပြီး အလုပ်လုပ်နေတာ တွေ့ရမှာပါ။ မတူတာကတော့ params ကနေရယူတာမျိုး မဟုတ်တော့ပဲ resolver ကနေရယူလိုက်တာဖြစ်ပါတယ်။</p><h3>Project</h3><p>Password Manager app ရေးပါ။ နမူနာကို <a href="https://ng-password-manager.web.app/"><strong>ဒီမှာ</strong></a> ကြည့်ပါ။ အဖြေကို <a href="https://github.com/YeMinKo/angular-training/tree/main/password-manager"><strong>ဒီမှာ</strong></a> ရနိုင်ပါတယ်။ App data အားလုံးကို <strong>AES 256</strong> နဲ့ encrypt လုပ်ပြီးမှ localStorage မှာသိမ်းပေးပါ။ Encryption အတွက် <a href="https://www.npmjs.com/package/crypto-js"><strong>crypto-js </strong></a>ကိုအသုံးပြုထားပါတယ်။ အောက်ပါအတိုင်း install လုပ်ပေးပါ။</p><pre>npm install crypto-js<br>npm install @types/crypto-js</pre><p>Implementation နမူနာကို <a href="https://stackoverflow.com/questions/53478860/how-to-encrypt-and-decrypt-in-angular-6/53478922"><strong>ဒီမှာ</strong></a> ကြည့်နိုင်ပါတယ်။</p><p><strong>Previous: </strong><a href="https://medium.com/learn-ng/route-guards-ee511df9fb71"><strong>Route Guards</strong></a></p><p><strong>Next: </strong><a href="https://medium.com/learn-ng/observable-66fd5ab0d438"><strong>Observable</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ce9b349ca831" width="1" height="1" alt=""><hr><p><a href="https://medium.com/learn-ng/more-in-routing-ce9b349ca831">More in Routing</a> was originally published in <a href="https://medium.com/learn-ng">Learn Ng</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>