<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Angular Archives - Blog IT</title>
	<atom:link href="https://blogit.create.pt/tag/angular/feed/" rel="self" type="application/rss+xml" />
	<link>https://blogit.create.pt/tag/angular/</link>
	<description>Create IT blogger community</description>
	<lastBuildDate>Thu, 05 May 2022 11:31:16 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>
	<item>
		<title>Handling not found routes in Angular</title>
		<link>https://blogit.create.pt/pedrolopes/2022/05/05/handling-not-found-routes-in-angular/</link>
					<comments>https://blogit.create.pt/pedrolopes/2022/05/05/handling-not-found-routes-in-angular/#respond</comments>
		
		<dc:creator><![CDATA[Pedro Lopes]]></dc:creator>
		<pubDate>Thu, 05 May 2022 11:31:15 +0000</pubDate>
				<category><![CDATA[Angular]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[404]]></category>
		<guid isPermaLink="false">https://blogit.create.pt/?p=12669</guid>

					<description><![CDATA[<p>In this post, we will see how we can handle 404 routes using a not found component in an Angular. There are two types of 404 routes that we want to handle. Static 404 &#8211; the URL the user navigates to doesn&#8217;t match any route Dynamic 404 &#8211; the URL matches a route, but the [&#8230;]</p>
<p>The post <a href="https://blogit.create.pt/pedrolopes/2022/05/05/handling-not-found-routes-in-angular/">Handling not found routes in Angular</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>In this post, we will see how we can handle 404 routes using a not found component in an Angular.</p>



<p>There are two types of 404 routes that we want to handle.</p>



<ul class="wp-block-list"><li><strong>Static 404</strong> &#8211; the URL the user navigates to doesn&#8217;t match any route</li><li><strong>Dynamic 404</strong> &#8211; the URL matches a route, but the data associated with that route does not exist. For example, you navigate to a detail page with an ID, but no item exists with that ID.</li></ul>



<p>For this post, I assume that there is a component named <code>NotFoundComponent</code> that is the not found page to show the user. Replace this with whatever component</p>



<h2 class="wp-block-heading">Static 404 routes</h2>



<p>To set up a static 404 page, you need to add two routes to your <code>RoutingModule</code> configuration.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
const routes: Routes = &#x5B;
    // Other routes
    { path: &#039;404&#039;, component: NotFoundComponent },
    { path: &#039;**&#039;, component: NotFoundComponent }
]
</pre></div>


<p>We used a catchall route <code><em>'</em>**'</code> that matches whatever route didn&#8217;t match above, and shows our not found page.</p>



<p>Make sure to keep the catchall route at the end of your routes, otherwise the routes below it will not be accessible.</p>



<p>Another option here would&#8217;ve been to redirect the user to the <code>'404'</code> route, but that way you lose the context of the URL. If the user navigates back, he would go to the non-existing route and be redirected to the <code>'404'</code> path again. If this is the behavior you want, switch the last route with <code>{ path: '**', redirectTo: '404' }</code>.</p>



<h2 class="wp-block-heading">Dynamic 404 routes</h2>



<p>Now that we have the static 404 route configured, we can use the router to navigate to it whenever we want to show the not found page to the user.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
this.router.navigate(&#x5B;&#039;/404&#039;], { skipLocationChange: true });
</pre></div>


<p>By using the option <code>skipLocationChange: true</code>, we have the same behavior as mentioned above. Even though the router navigates to the route, the browser URL stays the same, allowing the user to easily go back.</p>



<h3 class="wp-block-heading">Personalizing the 404 message</h3>



<p>You may want to personalize the message shown on the 404 page. One way to do this is using the navigation state.</p>



<p>When navigating to the 404 page, you can include a <code>state</code> in the navigation options.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
this.router.navigate(&#x5B;&#039;/404&#039;], { 
    skipLocationChange: true,
    state: {
        // Whatever data you need on the 404 page
    	source: &#039;article&#039;,
        id: 123456
    }
});
</pre></div>


<p>On your <code>NotFoundComponent</code>, you can access this object in the constructor using the router&#8217;s <a href="https://angular.io/api/router/Router#getcurrentnavigation"><code>getCurrentNavigation</code></a> method.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
export class NotFoundComponent {
  constructor(private router: Router) {
    console.log(this.router.getCurrentNavigation().extras.state.source);
  }
}
</pre></div>


<p>You must access the state in the constructor, as it is only available while the router is navigating.</p>



<h2 class="wp-block-heading">Conclusion</h2>



<p>In this post, we saw how to implement a 404 route in Angular. We looked at how we can redirect to it dynamically and even how to personalize the 404 page with route dependent data!</p>



<p>Have questions, corrections, or an alternative way of handling this? Leave a comment or reach out to me on Twitter <a href="https://twitter.com/pedronavelopes">@pedronavelopes</a> and let me know! Thanks for reading!</p>
<p>The post <a href="https://blogit.create.pt/pedrolopes/2022/05/05/handling-not-found-routes-in-angular/">Handling not found routes in Angular</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/pedrolopes/2022/05/05/handling-not-found-routes-in-angular/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Adding an Angular app to your Umbraco Website</title>
		<link>https://blogit.create.pt/marionunes/2018/03/10/adding-an-angular-app-to-your-umbraco-website/</link>
					<comments>https://blogit.create.pt/marionunes/2018/03/10/adding-an-angular-app-to-your-umbraco-website/#comments</comments>
		
		<dc:creator><![CDATA[Mário Nunes]]></dc:creator>
		<pubDate>Sat, 10 Mar 2018 22:11:23 +0000</pubDate>
				<category><![CDATA[Angular]]></category>
		<category><![CDATA[Umbraco]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[.NET]]></category>
		<guid isPermaLink="false">http://blogit.create.pt/marionunes/?p=334</guid>

					<description><![CDATA[<p>What problems does this solve? This enables you to have an Angular application alongside your Umbraco website, on a specified route. The Angular App can be protected by the Authentication of your website, if you have it. And you can make use of surface controllers for authenticated requests. The routing of your app will work [&#8230;]</p>
<p>The post <a href="https://blogit.create.pt/marionunes/2018/03/10/adding-an-angular-app-to-your-umbraco-website/">Adding an Angular app to your Umbraco Website</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><strong>What problems does this solve?</strong></p>
<ul>
<li>This enables you to have an Angular application alongside your Umbraco website, on a specified route.</li>
<li>The Angular App can be protected by the Authentication of your website, if you have it. And you can make use of surface controllers for authenticated requests.</li>
<li>The routing of your app will work as intended, for example, if your Angular app is at <strong>/app</strong> and you make a request to <strong>/app/products</strong>, the request will be routed to the angular app products&#8217; page.</li>
</ul>
<p><strong>What this is not</strong></p>
<ul>
<li>A complete front end for a Umbraco website with dynamic routes.</li>
<li>This doesn&#8217;t have dynamic routes, you have to specify them on your code.</li>
</ul>
<p><strong>Let&#8217;s get started!</strong></p>
<p>First, start by creating a controller to handle the Angular App request.</p>
<pre class="brush: csharp; title: ; notranslate">
using System.Web.Mvc;

public class AngularAppController : BaseController
{
    public ActionResult AngularAppView()
    {
        return View();
    }
}
</pre>
<p>Then, you have to decide the route where your Angular app will render. On this example it will be <strong>/angularapp</strong>.</p>
<p>On your ApplicationStarted method, put this code:</p>
<pre class="brush: csharp; title: ; notranslate">
RouteTable.Routes.MapRoute(
  &quot;AngularApp&quot;,
  &quot;angularapp/{*.}&quot;,
  new { controller = &quot;AngularApp&quot;, action = &quot;AngularAppView&quot; });
</pre>
<p>The <strong>{*.}</strong> syntax is what makes your angular app routes load and not return the 404 page.</p>
<p>If you don&#8217;t have an ApplicationStarted method, find out how you to create it here: <a href="https://our.umbraco.org/documentation/reference/events/application-startup">https://our.umbraco.org/documentation/reference/events/application-startup</a></p>
<p>Under your Views folder, create a folder with the same name as the <strong>controller</strong> you set in MapRoute. In this case it will be &#8220;AngularApp&#8221;.<br />
Inside it, create a view with the same name as the <strong>action</strong> set you set in MapRoute.<br />
<img decoding="async" src="http://blogit-create.com/wp-content/uploads/2018/03/appview.png" alt="" width="268" height="71" class="alignnone size-full wp-image-414" srcset="https://blogit.create.pt/wp-content/uploads/2018/03/appview.png 268w, https://blogit.create.pt/wp-content/uploads/2018/03/appview-265x71.png 265w" sizes="(max-width: 268px) 100vw, 268px" /></p>
<p>Inside the view put the contents of the index.html of your angular app.<br />
Then you will need to move your angular app files somewhere like you /Assets folder, and edit their paths on the view.</p>
<p>Almost done!</p>
<p>Now you need to edit the routes of your Angular app.<br />
Simply add the route you chose as a prefix to the routes.</p>
<p>From something like this:</p>
<pre class="brush: csharp; title: ; notranslate">
const routes: Routes = &#x5B;
  { path: '', redirectTo: '/dashboard', pathMatch: 'full' },
  { path: '/dashboard', component: DashboardComponent },
  { path: '/detail/:id', component: HeroDetailComponent },
  { path: '/heroes', component: HeroesComponent }
];
</pre>
<p>To this:</p>
<pre class="brush: csharp; title: ; notranslate">
const routes: Routes = &#x5B;
  { path: 'angularapp', redirectTo: 'angularapp/dashboard', pathMatch: 'full' },
  { path: 'angularapp/dashboard', component: DashboardComponent },
  { path: 'angularapp/detail/:id', component: HeroDetailComponent },
  { path: 'angularapp/heroes', component: HeroesComponent }
];
</pre>
<p>Thanks for reading!<br />
Mário</p>
<p>The post <a href="https://blogit.create.pt/marionunes/2018/03/10/adding-an-angular-app-to-your-umbraco-website/">Adding an Angular app to your Umbraco Website</a> appeared first on <a href="https://blogit.create.pt">Blog IT</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blogit.create.pt/marionunes/2018/03/10/adding-an-angular-app-to-your-umbraco-website/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
