avatarHailey

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

5140

Abstract

tr">showHello</span>: <span class="hljs-built_in">boolean</span> = <span class="hljs-literal">true</span>; }</pre></div><p id="30ba">The code above will render:</p><ul><li>the <code><h2>Hello</h2></code> element if the <code>showHello</code> property is <code>true</code></li><li>the <code><h2>Goodbye</h2></code> element if the <code>showHello</code> property is <code>false</code></li></ul><p id="ea45">This is visually much more like the <code>if-else</code> syntax in JavaScript.</p><h1 id="a8c5">The @if else if else syntax</h1><p id="8953">Angular is not yet done. They also added support for <code>else if</code> conditions. This is something that was not possible with the previous <code>ngIf</code> syntax.</p><p id="8702">Let’s see an example:</p><div id="4194"><pre><span class="hljs-meta">@Component</span>({ <span class="hljs-attr">template</span>: <span class="hljs-string"> @if (showHello) { &lt;h2&gt;Hello&lt;/h2&gt; } @else if (showGoodbye) { &lt;h2&gt;Goodbye&lt;/h2&gt; } @else { &lt;h2&gt;See you later&lt;/h2&gt; } </span>, }) <span class="hljs-keyword">class</span> <span class="hljs-title class_">Test</span> { <span class="hljs-attr">showHello</span>: <span class="hljs-built_in">boolean</span> = <span class="hljs-literal">true</span>; <span class="hljs-attr">showGoodbye</span>: <span class="hljs-built_in">boolean</span> = <span class="hljs-literal">false</span>; }</pre></div><p id="3078">The code above will:</p><ul><li>render the <code><h2>Hello</h2></code> element if the <code>showHello</code> property is <code>true</code></li><li>the <code><h2>Goodbye</h2></code> element if the <code>showGoodbye</code> property is <code>true</code> and <code>showHello</code> is <code>false</code></li><li>the <code><h2>See you later</h2></code> element if both the <code>showHello</code> and <code>showGoodbye</code> properties are <code>false</code>.</li></ul><p id="1b8e">Again, this is so close to what we would write with just plain Javascript.</p><h1 id="e632">Why don’t we need to import @if?</h1><p id="e45f">Notice that we don’t have to import the <code>@if</code> directive from <code>@angular/common</code> into our component templates anymore.</p><p id="c38b">This is because the <code>@if</code> syntax is part of the template engine itself, and it is <b>not</b> a directive.</p><p id="4a22">The new <code>@if</code> is built-in directly into the template engine, so it's automatically available everywhere.</p><p id="0abb">It’s just like the <code>{{variable}}</code> interpolation syntax or the <code>i18n</code> syntax - you don't need to import anything in order to use it.</p><h1 id="7a63">Why don’t we need to use the * syntax anymore?</h1><p id="7930">The <code></code> syntax was only used because <code>ngIf</code> was a structural directive. The <code></code> syntax was just syntactical sugar to make it easier for developers to use structural directives.</p><p id="4e2d">You can read more about how the old <code></code> syntax works under the hood <a href="https://blog.angular-university.io/angular-ngif/">here</a>.</p><p id="a005">The bottom line is that with <code>@if</code> we don't need to use the <code>*</code> syntax anymore, because <code>@if</code> is <i>not</i> a structural directive.</p><h1 id="b9f3">Why is the new @if syntax better than ngIf?</h1><p id="e988">Let’s quickly summarize the main reasons why <code>@if</code> is better than <code>*ngIf</code>:</p><ul><li>less verbose, more intuitive</li><li>no need for imports</li><li>supports <code>else if</code> and <code>else</code> conditions</li><li>no runtime overhead</li><li>will make the future evolution of the framework easier</li></ul><h1 id="128e">How to easily migrate to the new @if syntax?</h1><p id="ecdd">If you are using older versions of Angular, you can migrate to the new <code>@if</code> syntax using the Angular CLI.</p><p id="5647">The Angular CLI has an automated migration for upgrading all your code to the new <code>@if</code> syntax:</p><div id="abf9"><pre>ng generate <span class="hljs-keyword">@angular</span>/<span class="hljs-attribute">core</span>:control-flow</pre></div><p id="b039">This command will migrate all the <code>*ngIf</code> directives in your project to the new syntax, not only for <code>@if</code> but also for the <code>@for</code> and <code>@switch</code> syntax.</p><h1 id="303a">How does @if compare to hiding elements using just plain CSS?</h1><p id="f799">In HTML, we can hide elements by manipulating their <code>display</code> and <code>visibility</code> properties:</p><ul><li>if we set the <code>display</code> of an element to <code>none</code> the document is not visible in the document</li><li>when the <code>visibility</code> of an element is set to <code>hidden</code>, the element is not shown.</li></ul><p id="3a11">These all may look similar because they give the same results, but they are <b>not</b> the same.</p><p id="b1a7">In both cases, the hidden element still exists in the DOM, while with <code>@if</code> the hidden element doesn't

Options

exist on the DOM <b>at all</b>.</p><h1 id="71c4">Multiple nested @if async pipe anti-pattern</h1><p id="7019">Remember in the beginning we talked about an <code>@if</code> anti-pattern?</p><p id="95cd">If you use RxJs in your code, I think this is well worth mentioning it because you will see it a lot, and it’s directly related to <code>@if</code>.</p><p id="09ed">This has to do with the usage of the <code>@if</code> syntax not really to show or hide an element, but just to access the values of an observable using the <code>async</code> pipe.</p><p id="e628">This anti-pattern is sometimes known as “async pipe chaining” or “pyramid of doom”.</p><p id="e668">Here is an example of this <code>@if</code> anti-pattern:</p><div id="f343"><pre><span class="hljs-meta">@if</span> (user | <span class="hljs-keyword">async</span>; <span class="hljs-keyword">as</span> user) { .... <span class="hljs-meta">@if</span> (course | <span class="hljs-keyword">async</span>; <span class="hljs-keyword">as</span> course) { .... <span class="hljs-meta">@if</span> (lessons | <span class="hljs-keyword">async</span>; <span class="hljs-keyword">as</span> lesson) { .... } } }</pre></div><p id="293c">As you can see, we are using async pipe to unwrap the values of different Observables and assign them to a local template variable.</p><p id="b316">Then we are using the local template variable in the next <code>@if</code> statement.</p><p id="e4f9">We are repeating the use of <code>@if</code> and the async pipe, at multiple levels of the page, just for the purpose of accessing data, and nothing more.</p><p id="3fe5">This practice makes the code hard to read and maintain if several nesting levels are needed.</p><p id="4688">Besides, it makes the code harder to refactor, if you are doing this locally in several parts of the page instead of the whole page.</p><p id="d9ce">A good way to avoid this is to refactor your component so that it provides one single <code>data</code> observable, containing all the data that the page needs.</p><p id="83c5">Start by defining one interface that contains all the data that the page needs:</p><div id="9835"><pre><span class="hljs-keyword">interface</span> <span class="hljs-title class_">PageData</span> { <span class="hljs-attr">user</span>: <span class="hljs-title class_">User</span>; <span class="hljs-attr">course</span>: <span class="hljs-title class_">Course</span>; <span class="hljs-attr">lessons</span>: <span class="hljs-title class_">Lesson</span>[]; }</pre></div><p id="c697">Then, define a single <code>data$</code> observable that contains all the data that the page needs:</p><div id="817f"><pre><span class="hljs-meta">@Component</span> <span class="hljs-keyword">export</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">Component</span> <span class="hljs-keyword">implements</span> <span class="hljs-title class_">OnInit</span> {

<span class="hljs-keyword">private</span> <span class="hljs-attr">data$</span>: <span class="hljs-title class_">Observable</span><<span class="hljs-title class_">PageData</span>>;

<span class="hljs-title function_">ngOnInit</span>(<span class="hljs-params"></span>) {

<span class="hljs-keyword">const</span> user$ = <span class="hljs-comment">// ... initialize user$ observable</span>

<span class="hljs-keyword">const</span> course$  = <span class="hljs-comment">// ... initialize course$ observable</span>

<span class="hljs-keyword">const</span> lessons$ = <span class="hljs-comment">// ... initialize lessons$ observable    </span>

<span class="hljs-variable language_">this</span>.<span class="hljs-property">data$</span> = <span class="hljs-title function_">combineLatest</span>([user$, course$, lessons$])
  .<span class="hljs-title function_">pipe</span>(
    <span class="hljs-title function_">map</span>(<span class="hljs-function">(<span class="hljs-params">[user, course, lessons]</span>) =&gt;</span> {
      <span class="hljs-keyword">return</span> {
            user, 
            course, 
            lessons
        }
    })
);

}

}</pre></div><p id="54d1">The operator <code>combineLatest</code> is just one way to create this combined Observable.</p><p id="36d9">Finally, use <code>@if</code> in combination with the <code>async</code> pipe to unwrap the <code>data</code> observable in the template:</p><div id="6972"><pre><span class="hljs-meta">@if</span> (<span class="hljs-keyword">data</span> | async; <span class="hljs-keyword">as</span> <span class="hljs-keyword">data</span>) { .... {{<span class="hljs-keyword">data</span>.course}}

{{<span class="hljs-keyword">data</span>.user}}

{{<span class="hljs-keyword">data</span>.lessons}}

} </pre></div><p id="1fc8">As you can see, this removes all the unnecessary <code>@if</code> nesting and makes the code much more readable and maintainable.</p><p id="b721">If you apply this at the beginning of the template, you will have all your data easily available everywhere.</p></article></body>

8 Past Controversies Of King Charles III Which Made Him Infamous

The lesser-known truth.

Source: Wiki

The British monarchy is perhaps the oldest and the most famous around the globe.

There are those who are loved by everyone, like Prince William and his wife Kate Middleton .

However, the same can’t be said for Charles, who has just stepped in the shoes of his late mother, Queen Elizabeth II.

Already we are seeing a backlash from the public and largely because of the controversies he has been a part of.

From the way he is involved in politics to his relationship with ate Princess Diana and everything in between — Charles has been more of an estranging character of the British Royal Family.

The most interesting part is most of his controversies have been linked to the Royal family itself, from how he treats his current family and especially during the time he was married to Diana.

Below you will find King Charles’ most eyebrow-raising moments of all time — let us just hope his days as King won’t be as they were when he was a prince.

1. Being Involved In Politics

(Image Source: Wikipedia)

Members of the Royal Family are supposed to keep themselves away from politics, and it is kind of compulsory to stay politically neutral, especially after the British Royal Family transferred their power to the parliament.

The Queen did an exceptional job when it came to keeping herself away from all the political controversies.

However, the same can’t be said about King Charles III, who is often not shy about his opinions on political matters of the country — King has often been criticized in the past for this and possibly his future as King.

2. The Black Spider Memos

(Image Source: Wikipedia)

In 2015, the Black Spider Memos created quite a fuzz for Charles.

There was a total of 27 memos that Charles sent in 2004 and 2005, in which Charles was directly lobbying British Prime Minister of that time Tony Blair and other government officials for a big policy change.

These changes included promoting alternative medicine and replacing military helicopters.

His views were challenged by many parliamentarians, and according to most, his views were rather absurd.

Not only were his views challenged scientifically, but they were also being laughed at, while those who said these views had no more weight than a man sitting at a pub.

Still, some parliamentarians complained his views were given serious priority — such that they did not deserve it.

3. Things That Diana Revealed

(Image Source: Wikipedia)

In 1995 Lady Diana gave an interview to Panorama, which earned its place in history for a lot of reasons.

Diana did spill the beans in that interview about her relationship with the Royal family and things between her and Charles — which weren’t going well at all.

In the same interview, Diana commented about how miserable she was after being married to the Prince and about Charles’ infidelity, which wasn’t positive for him and his future.

4. King Charles’s Reaction To Harry’s Birth

Source: Wiki

Things were already tense between the two even before Harry’s Birth. After the heir to the throne was secured, Charles was hoping the second child would be a girl, which we all know, it wasn’t.

In the Biography, Diana said she knew she was carrying a boy in her belly, but she preferred to keep it a secret until after Harry was born.

However, what was really shocking for her was his first reaction to Charles, where he said Oh God! It’s a boy.

5. Taj Mahal Moment With Diana

Source: Wiki

The incident at the iconic Taj Mahal doesn’t really show Charles in any positive light.

Taj Mahal was planned well before the couple even arrived in India, and being the most loving couple at the time, it was just right to pose in front of a monument which symbolizes love.

However, Charles being the man he was, didn’t show up at the Taj Mahal, and a picture of a neglected, lonely Princess was published — a girl who didn’t have anyone to love broke the hearts of thousands.

Later a biographer claimed that Diana knew exactly what she was doing, and she knew that this would have an effect on Charles’s status as Prince, but this also tells us that things went bad between both really early in the marriage.

6. Prince William’s Hospitalization

Prince William was hospitalized in 1991 while he was away at a boarding school. William got into an accident that involved a golf club to the head, injuries that William was significant enough to require surgery.

Diana stayed beside William’s bed until he was fully conscious, and we’ll, but Charles prefered returning to his “Royal Duties”, which was to attend the OPERA!

And as absurd as it sounds now, it was the same for the people during those days, and this move of his didn’t really earn him any applause.

7. Charles Wedding To Camilla

In an interview with a TV channel, Charles talked about Charles and Diana’s separation.

Charles admitted to an affair that he had with Camilla; he said that he has been unfaithful to Diana — which also confirmed everything Diana claimed about Charles and his infidelity.

Charles was back in the news again in 2005 when he got married to Camilla, a ceremony which was kept really low-key.

Both Charles and Camilla got married in a civil ceremony that Queen Elizabeth didn’t attend. However, she did show up at the reception of the newlywed couple.

8. From Engagement To Divorce

(Image Source: Wikipedia)

Charles’ and Diana’s engagement was the biggest royal news around the world.

However, their marriage didn’t really get off with the best of starts. In an interview that the couple gave after their engagement in 1981.

Charles was asked if he loved Diana, to which Charles’s answer was: whatever love means, which not only caused many to give his answer a second thought but also severely traumatized Princess Diana.

Things just kept on getting worse for the couple, and even when divorce was not seen as something ideal, both had to opt for it because things got to the point of no return.

After Charles and Diana lived in separation for more than four years, their marriage was legally concluded in the August of 1996.

Final Words

We all make mistakes in our lives, but the best among us are those who learn from their mistakes.

Now we have to wait and see what the new King will bring us and if his reign is going to be as magnificent as his late mother or…

More from the author:

History
Life
Culture
Politics
Education
Recommended from ReadMedium