{"id":693,"date":"2023-11-28T03:48:00","date_gmt":"2023-11-28T03:48:00","guid":{"rendered":"https:\/\/staging.infragistics.com\/blogs\/?p=693"},"modified":"2025-02-19T07:13:50","modified_gmt":"2025-02-19T07:13:50","slug":"control-flow-in-angular-17","status":"publish","type":"post","link":"https:\/\/www.infragistics.com\/blogs\/control-flow-in-angular-17","title":{"rendered":"Control Flow In Angular 17: Elevate Your Development Experience"},"content":{"rendered":"\n<p>I&#8217;m excited to share that you can enhance your Angular development experience within <a href=\"\/products\/ignite-ui-angular\">Ignite UI for Angular<\/a> components by embracing the new Block Template Syntax for optimized, built-in control flow. This powerful feature simplifies your code, improves type checking, reduces bundle size, and boosts performance. So now, let&#8217;s dive into some code samples to see the syntax in action!<\/p>\n\n\n\n<p class=\"text--align-center\"><a class=\"trackBlogCTA ui-btn ui-btn--default ui-btn--sm\" title=\"Button \/ Ignite UI for Angular \/ Try It Now\" href=\"\/products\/ignite-ui-angular\/download\">Try Ignite UI for Angular<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-built-in-control-flow\">What Is Built-in Control Flow in Angular?<\/h2>\n\n\n\n<p>The term &#8220;Control Flow&#8221; in Angular refers to the order in which statements and directives are executed within an Angular application. In Angular 17, there is a new optimized built-in syntax to manage the control flow (@if, @switch, @for), offering developers finer control over the execution flow. This brings developers closer to familiar patterns in most programming languages, minimizing errors in constructing application views. It is directly available and ready to use in templates without additional imports.<\/p>\n\n\n\n<p>In Angular 17, Control Flow is managed by the refined change detection system, efficiently applying application state changes to the view. This underscores Angular&#8217;s role as a provider of a solid foundation for building modern, interactive web applications.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conditional-statements\">Starting Off With Conditional Statements&nbsp;<\/h2>\n\n\n\n<p>What you need to do is upgrade your *ngIf and *ngSwitch with the new syntax for a more intuitive and cleaner code.&nbsp;<\/p>\n\n\n\n<p>@if<\/p>\n\n\n\n<p>Utilize the @if within an IgxColumn template of the IgxGrid for conditional rendering:&nbsp;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;igx-column field=\"Discontinued\" header=\"Discontinued\" [sortable]=\"true\" [dataType]=\"'boolean'\"> \n    &lt;ng-template igxCell let-cell=\"cell\" let-val> \n        @if (val) { \n            &lt;img src=\"assets\/active.png\" title=\"Continued\" alt=\"Continued\" \/>\n        } @else { \n            &lt;img src=\"assets\/expired.png\" title=\"Discontinued\" alt=\"Discontinued\" \/> \n        } \n    &lt;\/ng-template> \n&lt;\/igx-column> <\/pre>\n\n\n\n<p><a title=\"Demo @if in IgxColumn\" href=\"https:\/\/codesandbox.io\/p\/devbox\/conditional-rendering-if-p8v6yk\" target=\"_blank\" rel=\"noopener noreferrer\">Demo @if in IgxColumn<\/a><\/p>\n\n\n\n<p>@switch&nbsp;<\/p>\n\n\n\n<p>Apply @switch directive within an IgxSelectItem of the IgxSelect component for dynamic icon selection:&nbsp;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;igx-select-item [value]=\"fruit.type\" class=\"select__entry\" [text]=\"fruit.type\">\n    {{fruit.type}}\n    @switch (fruit.delivery) {\n    @case ('flight') { &lt;igx-icon>flight&lt;\/igx-icon> }\n    @case ('train') { &lt;igx-icon>train&lt;\/igx-icon> }\n    @case ('boat') { &lt;igx-icon>directions_boat&lt;\/igx-icon> }\n    }\n&lt;\/igx-select-item><\/pre>\n\n\n\n<p><a title=\"Demo @switch in IgxSelect\" href=\"https:\/\/codesandbox.io\/p\/devbox\/conditional-rendering-switch-dh8y3q\" rel=\"noopener\">Demo @switch in IgxSelect<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"built-in-for-loop\">Let&#8217;s Talk About The Built-In for Loop Next<\/h2>\n\n\n\n<p>Experience a faster rendering speed with the new built-in for loop.<\/p>\n\n\n\n<p>@for&nbsp;<\/p>\n\n\n\n<p>A lot of use cases can be figured out with this. The example here uses the IgxDropDown:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@for (town of towns | startsWith:townSelected; track town.name) {\n    &lt;igx-drop-down-item [value]=\"town\">\n        {{town}}\n    &lt;\/igx-drop-down-item>\n}<\/pre>\n\n\n\n<p><a title=\"Demo For Loop in IgxDropDown\" href=\"https:\/\/codesandbox.io\/p\/devbox\/built-in-for-loop-drop-down-d2gft3\" target=\"_blank\" rel=\"noopener noreferrer\">Demo For Loop in IgxDropDown<\/a>&nbsp;<\/p>\n\n\n\n<p>Or looping the columns of the grid:&nbsp;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@for(c of columns; track c.field) {\n    &lt;igx-column\n    [field]=\"c.field\"\n    [header]=\"c.field\"\n    [cellStyles]=\"c.cellStyles\">\n    &lt;\/igx-column>\n}<\/pre>\n\n\n\n<p><a title=\"Demo For Loop in IgxGrid\" href=\"https:\/\/codesandbox.io\/p\/devbox\/built-in-for-loop-grid-columns-f45c3s\" rel=\"noopener\">Demo For Loop in IgxGrid<\/a>&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"deferrable-view-for-lazy-loading\">Deferrable Views for Lazy Loading&nbsp;<\/h2>\n\n\n\n<p>Leverage the new deferrable views to enhance lazy loading with a declarative and powerful approach. This new feature can give you the ability to create a deferred loading of the list items of the IgxList components. They will appear after a button click:&nbsp;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;igx-list #fruitList>\n    @defer (on interaction(b1)) {\n        @for(fruit of fruitsData; track fruit) {\n            &lt;igx-list-item igxRipple igxRippleTarget=\".igx-list__item-content\">\n                {{ fruit }}\n            &lt;\/igx-list-item>\n        }\n    } @placeholder {\n        &lt;button #b1 type=\"button\" igxButton=\"raised\">Click here to trigger interaction&lt;\/button>\n    }\n&lt;\/igx-list><\/pre>\n\n\n\n<p><a title=\"Demo deferrable view with IgxList\" href=\"https:\/\/codesandbox.io\/p\/devbox\/deferrable-views-list-4mty84\" target=\"_blank\" rel=\"noopener noreferrer\">Demo deferrable view with IgxList<\/a>&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"combined-sample\">Combined Sample&nbsp;<\/h2>\n\n\n\n<p>If you want, you can explore a more&nbsp;<a title=\"comprehensive example\" href=\"https:\/\/codesandbox.io\/p\/devbox\/comprehensive-example-sd8x4m\" target=\"_blank\" rel=\"noopener noreferrer\">comprehensive example<\/a> that integrates all the mentioned features.&nbsp;Here, we can defer the main content of the nav drawer. It will be loaded right after an item from the nav is selected:&nbsp;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@defer(on interaction(drawer)) {\n  &lt;span igxButton=\"icon\" igxToggleAction=\"navigation\">\n    &lt;igx-icon family=\"material\">menu&lt;\/igx-icon>\n  &lt;\/span>\n  &lt;h5>{{selected}}&lt;\/h5>\n  @switch(selected) {\n    @case('Grid') {\n      &lt;app-grid-adf-style-sample>&lt;\/app-grid-adf-style-sample>\n    }\n    @case('Autocomplete') {\n      &lt;app-autocomplete>&lt;\/app-autocomplete>\n    }\n    @default {\n      &lt;app-reactive-form-validation>&lt;\/app-reactive-form-validation>\n    }\n  }\n} @placeholder {\n  &lt;span>Pick a component from nav drawer to load...&lt;\/span>\n}<\/pre>\n\n\n\n<p>You can check out the network tab to see the application&#8217;s initial load time and resources. Since we are deferring the main content of the nav drawer, this is what is loaded:&nbsp;&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/static.infragistics.com\/marketing\/Blogs\/Migration\/00\/00\/00\/09\/08\/3683.defer-view-before-load.png\" alt=\"control flow in Angular\" title=\"check out the network tab to see the application's initial load time and resources\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>And once the user selects an item, the content and the resources for the view are being loaded:&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"http:\/\/static.infragistics.com\/marketing\/Blogs\/Migration\/00\/00\/00\/09\/08\/2287.defer-view-after-load.png\" alt=\" once the user selects an item, the content and the resources for the view are being loaded\" title=\"once the user selects an item, the content and the resources for the view are being loaded\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Depending on the size of the deferred views, you can increase your load time dramatically.&nbsp;More information on deferred views can be found <a href=\"https:\/\/blog.angular.io\/introducing-angular-v17-4d7033312e4b#a953\" rel=\"noopener\">here<\/a>.&nbsp;&nbsp;Also, the listing of the nav items can be achieved with the new syntax:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@for(item of navItems; track item.name) {\n  &lt;span igxDrawerItem igxRipple (click)=\"navigate(item)\">\n    &lt;igx-icon family=\"material\">{{ item.name }}&lt;\/igx-icon>\n    &lt;span>{{ item.text }}&lt;\/span>\n  &lt;\/span>\n}<\/pre>\n\n\n\n<p>Also, in one of the views from the sample there are reactive form inputs that are utilizing this feature to show the validations, icons and hints:&nbsp;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;igx-input-group>\n    &lt;label igxLabel for=\"password\">Password&lt;\/label>\n    &lt;input igxInput name=\"password\" [type]=\"showPassword ? 'text' : 'password'\" formControlName=\"password\" \/>\n    @if (password.errors?.minlength) {\n        &lt;igx-hint >Password should be at least 8 characters&lt;\/igx-hint>\n    }\n    @if (password.value) {\n        &lt;igx-icon igxSuffix (click)=\"showPassword = !showPassword\">\n            {{ togglePasswordVisibility }}\n        &lt;\/igx-icon>\n    }\n&lt;\/igx-input-group><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"getting-started\">Getting Started&nbsp;<\/h2>\n\n\n\n<p>Upgrade your Angular project to version 17 and try out the built-in control flow today. To use it in your existing projects.&nbsp;Explore the improved developer experience and boost the performance of your Angular application.&nbsp;&nbsp;<\/p>\n\n\n\n<p>Happy coding with the enhanced Angular control flow!<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"\/products\/ignite-ui-angular\/download\"><img decoding=\"async\" src=\"https:\/\/static.infragistics.com\/marketing\/Blog-in-content-ads\/Ignite-UI-Angular\/ignite-ui-angular-you-get-ad.gif\" alt=\"Ignite UI for Angular\"\/><\/a><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Do you want to elevate your development experience with Ignite UI for Angular? Try the new block template syntax for optimized, built-in control flow.<\/p>\n","protected":false},"author":64,"featured_media":1002,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-693","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular"],"_links":{"self":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/693","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/users\/64"}],"replies":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/comments?post=693"}],"version-history":[{"count":4,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/693\/revisions"}],"predecessor-version":[{"id":1919,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/693\/revisions\/1919"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/media\/1002"}],"wp:attachment":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/media?parent=693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/categories?post=693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/tags?post=693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}