<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Prayatna Bhattarai</title>
    <description>The latest articles on Forem by Prayatna Bhattarai (@prayatna).</description>
    <link>https://forem.com/prayatna</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F231250%2Ffd5b7731-039f-4f10-8263-da4242bfdbff.jpeg</url>
      <title>Forem: Prayatna Bhattarai</title>
      <link>https://forem.com/prayatna</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/prayatna"/>
    <language>en</language>
    <item>
      <title>Create dynamic nested menu items in Angular </title>
      <dc:creator>Prayatna Bhattarai</dc:creator>
      <pubDate>Tue, 04 Jan 2022 11:31:18 +0000</pubDate>
      <link>https://forem.com/prayatna/create-dynamic-nested-menu-items-in-angular-11hm</link>
      <guid>https://forem.com/prayatna/create-dynamic-nested-menu-items-in-angular-11hm</guid>
      <description>&lt;p&gt;I was looking for ways to create a dynamic menu where we get list of menu items from the api and I came across this &lt;a href="https://stackoverflow.com/questions/61271254/how-to-create-a-submenu-in-a-dynamic-dropdown-list-with-typescript-and-angular"&gt;stackoverflow question&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;This explains how to use &lt;code&gt;ngTemplateOutlet&lt;/code&gt; to call your own custom template to populate the menu item by referencing the same template again. &lt;/p&gt;

&lt;p&gt;if you have an array of &lt;code&gt;menuItems&lt;/code&gt; like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;menuItems&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MenuItem&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
 &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Home&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;href&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/home&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;},&lt;/span&gt;
 &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Articles&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;href&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/articles&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;children&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;News&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;href&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/articles/news&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Entertainment&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;href&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/articles/entertain&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
     &lt;span class="p"&gt;],&lt;/span&gt;
 &lt;span class="p"&gt;},&lt;/span&gt;
 &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;About&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;href&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/about&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;];&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I have used hardcoded value for &lt;code&gt;menuItems&lt;/code&gt; but this can be populated by getting the value from the api.&lt;/p&gt;

&lt;p&gt;So, on the HTML (view of the component) we only have to do is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;nav&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;ul&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"navigation-list"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;ng-template&lt;/span&gt; &lt;span class="na"&gt;#menuItemTemplate&lt;/span&gt; &lt;span class="na"&gt;let-items&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
   &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;*ngFor=&lt;/span&gt;&lt;span class="s"&gt;"let item of items"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"{{item.href}}"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;{{item.label}}&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;ng-container&lt;/span&gt; &lt;span class="na"&gt;*ngIf=&lt;/span&gt;&lt;span class="s"&gt;"item.children"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;ul&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"sub-menu"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;ng-container&lt;/span&gt; &lt;span class="na"&gt;*ngTemplateOutlet=&lt;/span&gt;&lt;span class="s"&gt;"menuItemTemplate; 
          context: { $implicit: item.children }"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/ng-container&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
     &lt;span class="nt"&gt;&amp;lt;/ng-container&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/ng-template&amp;gt;&lt;/span&gt;
    ... more code will go here  
  &lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/nav&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above, we have created our custom template using &lt;code&gt;&amp;lt;ng-template&amp;gt;&lt;/code&gt; that creates a reference &lt;code&gt;menuItemTemplate&lt;/code&gt;. The &lt;code&gt;let-items&lt;/code&gt; inside the ng-template tag creates a variable that will be replaced by the &lt;code&gt;$implicit&lt;/code&gt; value that we define inside the &lt;code&gt;context&lt;/code&gt; of &lt;code&gt;ngTemplateOutlet&lt;/code&gt;. Now, whenever a menu item has it's children then it will create the template by passing in the &lt;code&gt;item.children&lt;/code&gt; array.&lt;/p&gt;

&lt;p&gt;But how do we actually use the &lt;code&gt;menuItems&lt;/code&gt; that we created on the component? We just have to call the template again by simply replacing &lt;code&gt;... more code will go here&lt;/code&gt; with&lt;br&gt;
&lt;code&gt;&amp;lt;ng-container *ngTemplateOutlet="menuItemTemplate; context: { $implicit: menuItems }"&amp;gt;&amp;lt;/ng-container&amp;gt;&lt;/code&gt; by using the actual value of &lt;code&gt;menuItems&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And that's it. You will have your nested menu items, no matter how many children the &lt;code&gt;menuItems&lt;/code&gt; will hold. &lt;/p&gt;

&lt;p&gt;Would love to know more ways that you could create dynamic menus! please drop in on the comments&lt;/p&gt;

&lt;p&gt;P.S: the menu is not styled so feel free to use your imagination :p&lt;/p&gt;

</description>
      <category>angular</category>
      <category>recursion</category>
      <category>ngtemplate</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Http Subscription on RxJs : not to use complete as a finally substitute</title>
      <dc:creator>Prayatna Bhattarai</dc:creator>
      <pubDate>Tue, 16 Mar 2021 06:13:56 +0000</pubDate>
      <link>https://forem.com/prayatna/http-subscription-on-rxjs-not-to-use-complete-as-a-finally-substitute-5b7b</link>
      <guid>https://forem.com/prayatna/http-subscription-on-rxjs-not-to-use-complete-as-a-finally-substitute-5b7b</guid>
      <description>&lt;p&gt;Coming from the &lt;code&gt;promise&lt;/code&gt; land, I was never aware of how &lt;code&gt;.finally&lt;/code&gt; was substituted when subscribed to an observable.&lt;/p&gt;

&lt;p&gt;The easy way of how I was using was to add it on &lt;code&gt;complete&lt;/code&gt; the third &lt;code&gt;param&lt;/code&gt; of &lt;code&gt;subscribe&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;BUT&lt;/em&gt;&lt;/strong&gt; whenever an error occurs on the &lt;code&gt;http&lt;/code&gt; request the &lt;code&gt;subscribe&lt;/code&gt; never calls the complete method &lt;/p&gt;

&lt;p&gt;You can find more info &lt;a href="https://stackoverflow.com/questions/33783967/rxjs-observable-doesnt-complete-when-an-error-occurs"&gt;here&lt;/a&gt; and &lt;a href="https://github.com/angular/angular/issues/7865"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To use finally like we do when dealing with &lt;code&gt;Promises&lt;/code&gt;, we can use &lt;code&gt;finalize&lt;/code&gt; &lt;a href="https://www.learnrxjs.io/learn-rxjs/operators/utility/finalize"&gt;operator&lt;/a&gt; and pipe it through the &lt;code&gt;observable$&lt;/code&gt; like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;someService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fetchDataFromApi&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;finalize&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isBusy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="c1"&gt;// next&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="c1"&gt;// error&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="c1"&gt;// complete&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I did have a look of another easier method - to add a function at the end of &lt;code&gt;subscription&lt;/code&gt; by using &lt;code&gt;.add()&lt;/code&gt; but it is actually used to add a teardown method on the &lt;code&gt;subscription&lt;/code&gt; or add more subscription/child to your existing &lt;code&gt;subscription&lt;/code&gt;. So the better way would be to use &lt;code&gt;finalize&lt;/code&gt; as illustrated on the code above.&lt;/p&gt;

&lt;p&gt;If you find other ways then please do share it :)&lt;/p&gt;

</description>
      <category>rxjs</category>
      <category>angular</category>
      <category>promise</category>
      <category>observable</category>
    </item>
    <item>
      <title>Rename folder to lowercase - Git</title>
      <dc:creator>Prayatna Bhattarai</dc:creator>
      <pubDate>Thu, 28 Jan 2021 06:52:14 +0000</pubDate>
      <link>https://forem.com/studentedge/rename-folder-to-lowercase-git-347d</link>
      <guid>https://forem.com/studentedge/rename-folder-to-lowercase-git-347d</guid>
      <description>&lt;p&gt;We are always having issues renaming a folder to lower case because git does not tend to understand the difference even if we change the folder.&lt;/p&gt;

&lt;p&gt;The way we would rename is:&lt;/p&gt;

&lt;p&gt;First move the folder to another temp. folder&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git mv src/Home src/homes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then move the folder to the desired folder name:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git mv src/homes src/home
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally commit the change&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git commit -m "rename folder from Home to home
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you are running windows, the rename may not reflect on the windows explorer so we follow similar approach. -&amp;gt; rename to temp folder and rename it back again&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Rename-Item Home homes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;commit the changes and again run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Rename-Item homes home
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and finally push your changes.&lt;/p&gt;

&lt;p&gt;We run webpack so sometimes it caches the build so have to run a build for prod and then again switch back to the local dev build.&lt;/p&gt;

</description>
      <category>git</category>
    </item>
    <item>
      <title>Common Git commands</title>
      <dc:creator>Prayatna Bhattarai</dc:creator>
      <pubDate>Tue, 01 Dec 2020 06:37:50 +0000</pubDate>
      <link>https://forem.com/studentedge/common-git-commands-68a</link>
      <guid>https://forem.com/studentedge/common-git-commands-68a</guid>
      <description>&lt;p&gt;Few of the git command that we use at Student Edge:&lt;/p&gt;

&lt;h4&gt;
  
  
  Pulling from origin (note: master below can be replaced by any branch name)
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;git pull --rebase origin master&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Switching to an existing branch
&lt;/h4&gt;

&lt;p&gt;If you already have the branch locally you can just do&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git checkout MyOtherBranch&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;if you don't have the branch locally you need to do the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git fetch

git checkout -b MyOtherBranch remotes/origin/MyOtherBranch
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Creating a new branch
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;git checkout -b MyNewBranch&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Pushing your branch to origin
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;git push origin MyBranch&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Commit your code to your current branch.
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add --all :/

git commit -m 'WR-123 I changed something'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Make a tweak to your previous commit
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;git commit --amend&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After running the above it will open vim. Just type &lt;code&gt;:wq&lt;/code&gt; and it will rebase and commit it&lt;/p&gt;

&lt;h4&gt;
  
  
  Remove a commit from your branch (note: the hats are how many commits to go back)
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;git rebase -i HEAD^^&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;After running this it will open vim with all the commits listed, the number of &lt;code&gt;^&lt;/code&gt; will indicate how many commits it will show. Type &lt;code&gt;i&lt;/code&gt; which will put it in edit mode. Delete the commit lines you want to remove. Then press &lt;code&gt;Esc&lt;/code&gt; and then &lt;code&gt;:rw&lt;/code&gt; to rebase.&lt;/p&gt;

&lt;p&gt;Thanks to &lt;a href="http://danielvanheerden.com/my-gitconfig/"&gt;Dan&lt;/a&gt;, you can add this helper git file in the root of your user account folder (eg. C:\users\'myUser') and create your own git shortcuts (.gitconfig)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[user]name = Your user name
email = your@email.com

[alias]# Pretty text base branch tree
lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)&amp;lt;%an&amp;gt;%Creset' --abbrev-commit --date=relative

# When on master branch, force local master to be the same as origin
refreshmaster = checkout    -B master remotes/origin/master

# pretty diff of changes
diff = diff --word-diff=color

# After Fixing conflicts - OK to proceed with rebase
ok = rebase --continue

# Push the current branch upstream to it's namesake branch on origin
up = push origin HEAD

# Push the current branch upstream to it's namesake branch on origin with FORCE
upf = push -f origin HEAD

# Replace NEW stuff from master to my current branch
new = pull --rebase origin master

# Track all new changes
done = add --all :/

# Amend my last commit - request you to do "git add --all :/" or "done" first and then do `:wq` in vim to save and rebase
tweak = commit --amend

# Give a list of the last branches on my local git
lb = for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads/
changes = diff-tree --no-commit-id --name-only -r 

[push]
default = upstream
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>git</category>
    </item>
    <item>
      <title>How to remove local branches that have been closed on master</title>
      <dc:creator>Prayatna Bhattarai</dc:creator>
      <pubDate>Mon, 23 Nov 2020 06:20:36 +0000</pubDate>
      <link>https://forem.com/studentedge/how-to-remove-local-branches-that-have-been-closed-on-master-3pnk</link>
      <guid>https://forem.com/studentedge/how-to-remove-local-branches-that-have-been-closed-on-master-3pnk</guid>
      <description>&lt;p&gt;In Git Bash/ Git CLI run the commands as follow (from whichever branch you are currently on):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;git remote prune origin
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;checkout master&lt;/li&gt;
&lt;li&gt;update it&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;and run in Git Bash/ Git CLI:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git branch --merged | grep -v "\*" | xargs -n 1 git branch -
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it!&lt;/p&gt;

&lt;p&gt;To delete all local branches except master:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git branch | grep -v "master" | xargs git branch -D
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some commands to a further clean up: &lt;a href="http://everydayrails.com/2014/02/27/git-reset-clean.html"&gt;Git clean&lt;/a&gt;&lt;/p&gt;

</description>
      <category>git</category>
    </item>
    <item>
      <title>TIL: How to pass data to parent component on a hybrid Angular project</title>
      <dc:creator>Prayatna Bhattarai</dc:creator>
      <pubDate>Tue, 10 Nov 2020 14:07:58 +0000</pubDate>
      <link>https://forem.com/prayatna/til-how-to-pass-data-to-parent-component-on-a-hybrid-angular-project-jm5</link>
      <guid>https://forem.com/prayatna/til-how-to-pass-data-to-parent-component-on-a-hybrid-angular-project-jm5</guid>
      <description>&lt;p&gt;Unlike my previous &lt;a href="https://dev.to/prayatna/til-how-to-pass-data-to-child-component-on-a-hybrid-angular-project-4o7p"&gt;post&lt;/a&gt; where we passed data to a child component, we will pass data from a child component to an AngularJS (parent) component.&lt;/p&gt;

&lt;p&gt;As with an Angular component, I use an event emitter to emit as &lt;code&gt;@Output&lt;/code&gt; like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; 
&lt;span class="nx"&gt;totalCountEvent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;EventEmitter&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, I'm trying to get the total count to the parent component (AngularJS component) &lt;code&gt;subject&lt;/code&gt; and we place the listing component to the &lt;code&gt;subject.component.html&lt;/code&gt; as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
 ...
  &lt;span class="nt"&gt;&amp;lt;subject-listing&lt;/span&gt; 
   &lt;span class="na"&gt;(total-count-event)= 
        &lt;/span&gt;&lt;span class="s"&gt;"updateTotalEarnablePoints($event)"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/subject-listing&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the same way we do it on any of the Angular components, but the only difference is &lt;code&gt;(total-count-event)&lt;/code&gt;. We have to use kebab-casing.&lt;/p&gt;

&lt;p&gt;And on our angularjs module, we downgrade the &lt;code&gt;subject-listing.component.ts&lt;/code&gt; as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;directive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;subjectListing&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;downgradeComponent&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;component&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;SubjectListingComponent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;outputs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;totalCountEvent&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With that, you can easily use the totalCountEvent on the AngularJS &lt;code&gt;subject.component.html&lt;/code&gt; component.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>todayilearned</category>
    </item>
    <item>
      <title>TIL: How to use form builder to create groups in Angular</title>
      <dc:creator>Prayatna Bhattarai</dc:creator>
      <pubDate>Fri, 30 Oct 2020 14:40:56 +0000</pubDate>
      <link>https://forem.com/prayatna/til-how-to-use-form-builder-to-create-groups-in-angular-5emo</link>
      <guid>https://forem.com/prayatna/til-how-to-use-form-builder-to-create-groups-in-angular-5emo</guid>
      <description>&lt;p&gt;We can use the &lt;code&gt;formBuilder&lt;/code&gt; service to group all the form fields. I start with injecting &lt;code&gt;FormBuilder&lt;/code&gt; from &lt;code&gt;angular/forms&lt;/code&gt; and inject in my &lt;code&gt;contactComponent&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;FormBuilder&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/forms&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;contact&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;templateUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./contact.component.html&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;styleUrls&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./contact.component.css&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;ContactComponent&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;OnInit&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;fb&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;FormBuilder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since it is a contact form, I have name, email, and message as the form fields. So I create a form group as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt; &lt;span class="nx"&gt;contactForm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;group&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I then can eventually add validation for each of the fields&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;contactForm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fb&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;group&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Validators&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;required&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Validators&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;required&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Validators&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;required&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then you would basically use it in the input as&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;formControlName&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it. There seems to be a lot of things that you can do with the form, but that will be a post for another day.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>todayilearned</category>
    </item>
    <item>
      <title>TIL: How to create a custom pipe in Angular</title>
      <dc:creator>Prayatna Bhattarai</dc:creator>
      <pubDate>Thu, 29 Oct 2020 13:59:09 +0000</pubDate>
      <link>https://forem.com/prayatna/til-how-to-create-a-custom-pipe-in-angular-45bb</link>
      <guid>https://forem.com/prayatna/til-how-to-create-a-custom-pipe-in-angular-45bb</guid>
      <description>&lt;p&gt;To build a custom pipe, just create a class that implements PipeTransfrom like this:&lt;/p&gt;

&lt;h4&gt;
  
  
  transform-to-space.pipe.ts
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;TransformToSpace&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;PipeTransform&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As the class above implements PipeTransform, we are required to implement every property/method. In this case, we need to implement the &lt;code&gt;transform&lt;/code&gt; method. &lt;/p&gt;

&lt;p&gt;The custom pipe we are creating takes a character and converts that to space. The final class will look something like this:&lt;/p&gt;

&lt;h4&gt;
  
  
  transform-to-space.pipe.ts final
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Pipe&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;PipeTransform&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Pipe&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;transformToSpace&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;TransformToSpace&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;PipeTransform&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;symbol&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order to use the pipe we put this on the template like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;{{message.value | transformToSpace:'&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;'}}&amp;lt;/&lt;/span&gt;&lt;span class="na"&gt;p&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use the same name as assigned under&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Pipe&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;transformToSpace&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since &lt;code&gt;transform&lt;/code&gt; takes two param: &lt;code&gt;value&lt;/code&gt; in this case is &lt;code&gt;message.value&lt;/code&gt; and &lt;code&gt;symbol&lt;/code&gt; is defined after pipe name and colon like &lt;code&gt;transformToSpace:'&amp;lt;'&lt;/code&gt;. And that is how you create and use a custom pipe.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>todayilearned</category>
    </item>
    <item>
      <title>TIL: ngIf can also have an else clause in Angular</title>
      <dc:creator>Prayatna Bhattarai</dc:creator>
      <pubDate>Wed, 28 Oct 2020 13:17:40 +0000</pubDate>
      <link>https://forem.com/prayatna/til-ngif-can-also-have-an-else-clause-in-angular-2321</link>
      <guid>https://forem.com/prayatna/til-ngif-can-also-have-an-else-clause-in-angular-2321</guid>
      <description>&lt;p&gt;I have a &lt;code&gt;message&lt;/code&gt; component that makes an &lt;code&gt;async&lt;/code&gt; call to retrieve the message. In my template, I use the &lt;code&gt;async&lt;/code&gt; pipe to populate the view. So on the component, I have a &lt;code&gt;message$&lt;/code&gt; that gets the message from a service like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;message$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;messageService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getMessage&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and in my &lt;code&gt;message-component.html&lt;/code&gt; I have&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;*ngIf=&lt;/span&gt;&lt;span class="s"&gt;"message$ | async as message"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      {{message.value}}
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
-- other code
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But the issue here is, I don't want my view to be empty when the message is being retrieved. I can use the &lt;code&gt;else&lt;/code&gt; clause inside the &lt;code&gt;*ngIf&lt;/code&gt; statement and define a template that replaces the &lt;code&gt;div&lt;/code&gt; from above to show that something is loading. You can use a loading spinner or something similar, but for simplicity, I will just use a &lt;code&gt;div&lt;/code&gt; with a text. So my final view will be something like&lt;/p&gt;

&lt;h4&gt;
  
  
  message.component.html
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;*ngIf=&lt;/span&gt;&lt;span class="s"&gt;"message$ | async as message; else isBusy"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  {{message.value}}
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;ng-template&lt;/span&gt; &lt;span class="na"&gt;#isBusy&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Loading...&lt;span class="nt"&gt;&amp;lt;/ng-template&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it; hope that helps.&lt;/p&gt;

&lt;p&gt;PS: The method stated above may not always be the best way to show a loading spinner for an async call, if there is any better way of doing it please let me know. TIA&lt;/p&gt;

</description>
      <category>angular</category>
      <category>todayilearned</category>
    </item>
    <item>
      <title>TIL: How to use async pipe and populate mat-select</title>
      <dc:creator>Prayatna Bhattarai</dc:creator>
      <pubDate>Tue, 27 Oct 2020 13:13:12 +0000</pubDate>
      <link>https://forem.com/prayatna/til-how-to-use-async-pipe-and-populate-mat-select-m35</link>
      <guid>https://forem.com/prayatna/til-how-to-use-async-pipe-and-populate-mat-select-m35</guid>
      <description>&lt;p&gt;I was trying to use &lt;code&gt;async | pipe&lt;/code&gt; directly on the view to get all the country list and populate my &lt;code&gt;mat-select&lt;/code&gt; dropdown. The &lt;code&gt;countrySelector&lt;/code&gt; component has a &lt;code&gt;countryList$&lt;/code&gt; observable which gets all the country from a service like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt; &lt;span class="p"&gt;...&lt;/span&gt;
  &lt;span class="nx"&gt;ngOnInit&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;countryList$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;countryService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getCountries&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then in my view, I use &lt;code&gt;ng-container&lt;/code&gt; with an &lt;code&gt;*ngIf&lt;/code&gt; clause to check if the country list is available.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;ng-container&lt;/span&gt; &lt;span class="na"&gt;*ngIf=&lt;/span&gt;&lt;span class="s"&gt;"countryList$ | async as countryList"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I then use the &lt;code&gt;countryList&lt;/code&gt; and populate the &lt;code&gt;mat-select&lt;/code&gt; which uses &lt;code&gt;*ngFor&lt;/code&gt; to go through the list of countries and add a &lt;code&gt;mat-option&lt;/code&gt; for each of the item. It also has a poperty &lt;code&gt;onSelectionChange&lt;/code&gt; which fires an event when a selection is changed. My final view will look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;ng-container&lt;/span&gt; &lt;span class="na"&gt;*ngIf=&lt;/span&gt;&lt;span class="s"&gt;"countryList$ | async as countryList"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;mat-select&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;mat-option&lt;/span&gt; &lt;span class="na"&gt;*ngFor=&lt;/span&gt;&lt;span class="s"&gt;" let country of countryList 
     [value]="&lt;/span&gt;&lt;span class="na"&gt;country&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;
     &lt;span class="na"&gt;(onSelectionChange)=&lt;/span&gt;&lt;span class="s"&gt;"changeCurrentSelection($event, 
     country)"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;                  
    {{country.name}}
  &lt;span class="nt"&gt;&amp;lt;/mat-option&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;/mat-select&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ng-container&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way I can populate the mat-option without having to assign it to any variable on the component and directly use it in the view.&lt;/p&gt;

&lt;p&gt;Please let me know if there are any other better ways to do it. &lt;/p&gt;

</description>
      <category>angular</category>
      <category>todayilearned</category>
    </item>
    <item>
      <title>TIL: How to pass data from child to parent component using a template variable</title>
      <dc:creator>Prayatna Bhattarai</dc:creator>
      <pubDate>Mon, 26 Oct 2020 14:18:00 +0000</pubDate>
      <link>https://forem.com/prayatna/til-how-to-pass-data-from-child-to-parent-component-using-a-template-variable-1b5a</link>
      <guid>https://forem.com/prayatna/til-how-to-pass-data-from-child-to-parent-component-using-a-template-variable-1b5a</guid>
      <description>&lt;p&gt;Was trying to look for ways on how to pass data around from a child to a parent component and I found a very easy way - using a template variable.&lt;/p&gt;

&lt;p&gt;If you had followed my previous post, I used the &lt;code&gt;postMessage&lt;/code&gt; component to post a message that has been used on the child component i.e the &lt;code&gt;message&lt;/code&gt; component by using &lt;code&gt;Output()&lt;/code&gt;. But I have found a cleaner way. &lt;/p&gt;

&lt;h4&gt;
  
  
  post-message.component.html
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
    .. other stuff
    &lt;span class="nt"&gt;&amp;lt;message&lt;/span&gt; &lt;span class="na"&gt;#myMessage&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/message&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;{{myMessage.message.value}}&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;#myMessage&lt;/code&gt; is a template variable that you can use on the &lt;code&gt;postMessage&lt;/code&gt; component's view. So here, I could access any public field or methods of the &lt;code&gt;message&lt;/code&gt; component through the template variable.&lt;/p&gt;

&lt;h4&gt;
  
  
  message.component.ts
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;EventEmitter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;OnInit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Output&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@angular/core&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;templateUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./message.component.html&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;styleUrls&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./message.component.css&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;MessageComponent&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;OnInit&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;IMessage&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="nx"&gt;ngOnInit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//eg message - would basically come from user input though&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This is a message I like to post&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, whatever I assign to &lt;code&gt;this.message&lt;/code&gt; will have the value available on the &lt;code&gt;postMessage&lt;/code&gt; component which is the parent component of &lt;code&gt;message&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Hope that is handy. :D&lt;/p&gt;

</description>
      <category>angular</category>
      <category>todayilearned</category>
    </item>
    <item>
      <title>TIL: How to pass data from child to parent component in Angular</title>
      <dc:creator>Prayatna Bhattarai</dc:creator>
      <pubDate>Sun, 25 Oct 2020 13:07:59 +0000</pubDate>
      <link>https://forem.com/prayatna/til-how-to-pass-data-from-child-to-parent-component-in-angular-4p7c</link>
      <guid>https://forem.com/prayatna/til-how-to-pass-data-from-child-to-parent-component-in-angular-4p7c</guid>
      <description>&lt;p&gt;A simple way to pass data from the child to the parent component using &lt;code&gt;EventEmitter&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;I have a child component which holds a message object and I want to pass it back to the parent &lt;code&gt;postMessage&lt;/code&gt; component&lt;/p&gt;

&lt;h4&gt;
  
  
  message.component.ts
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;EventEmitter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;OnInit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Output&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@angular/core&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;templateUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./message.component.html&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;styleUrls&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./message.component.css&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;MessageComponent&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;OnInit&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;IMessage&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nx"&gt;postMessageEvent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;EventEmitter&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="nx"&gt;ngOnInit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//eg message - would basically come from user input though&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This is a message I like to post&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;postMessageEvent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you look closely, I have created an &lt;code&gt;@Output&lt;/code&gt; &lt;code&gt;postMessageEvent&lt;/code&gt; that emits the current &lt;code&gt;message&lt;/code&gt; when the &lt;code&gt;handleClick()&lt;/code&gt; function gets called from the view&lt;/p&gt;

&lt;h4&gt;
  
  
  message.component.html
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
  {{message.value}}
  &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;(click) = &lt;/span&gt;&lt;span class="s"&gt;handleClick()&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Post message&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is just a simple view that displays the message value and has a button that calls the &lt;code&gt;handleClick()&lt;/code&gt; function when pressed. It would be better to have an input field, but that is a topic for another day ;)&lt;/p&gt;

&lt;p&gt;Now back to the parent component. In order to get the message object, I call the &lt;code&gt;postMessageEvent&lt;/code&gt; through the view like so&lt;/p&gt;

&lt;h3&gt;
  
  
  post-message.component.html
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
 ...
 &lt;span class="c"&gt;&amp;lt;!-- other code --&amp;gt;&lt;/span&gt;
 &lt;span class="nt"&gt;&amp;lt;message&lt;/span&gt; &lt;span class="na"&gt;(postMessageEvent)=&lt;/span&gt;&lt;span class="s"&gt;postMessage($event)&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/message&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I use the same event name &lt;code&gt;postMessageEvent&lt;/code&gt; inside the brackets and assign that to a function &lt;code&gt;postMessage()&lt;/code&gt; on the component like&lt;/p&gt;

&lt;h3&gt;
  
  
  post-message.component.ts
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;OnInit&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@angular/core&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;post-message&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;templateUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./post-message.component.html&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;styleUrls&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./post-message.component.css&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;PostMessageComponent&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;OnInit&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="nx"&gt;ngOnInit&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="nx"&gt;postMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;messageFromChild&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;IMessage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;messageFromChild&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;messageFromChild&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;//call service/api to post message&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we emit the message from the child component, the &lt;code&gt;postMessage&lt;/code&gt; function takes a parameter which is basically the message object from the child. So when you click the button on the child component the &lt;code&gt;postMessage&lt;/code&gt; on the parent component gets fired and you have access to the current &lt;code&gt;message&lt;/code&gt; object And that's all. Now you can use your &lt;code&gt;messageFromChild&lt;/code&gt; to call a service/api or do anything you want.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>todayilearned</category>
    </item>
  </channel>
</rss>
