Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

June 16, 2017

Angular Learning Path

Filed under: Angular @ 4:06 pm

The content of this post is based on Angular version >= 2.x unless explicitly stated otherwise.

Are you looking for a path for learning Angular? Try these courses on Pluralsight:

Angular: Getting Started (5h 26 m)

This course gets you started with the basics of Angular including building components, services, and Angular modules.

Angular CLI (2h 26 m)

In this course, you will learn how to create, generate, lint, debug, test, build, and serve high quality apps that follow the Angular Style Guide using the Angular CLI.

Angular Reactive Forms (3h 54 m)

This course details how to build Reactive forms, validate user-entered data, and perform CRUD (Create, Read, Update, and Delete) operations using HTTP.

Angular Routing (4h 46 m)

In this course, you’ll learn how to define multiple routes, pass data to routes, guard your routes, preload data for your views, lazy load routes for better performance and more.

Angular Fundamentals (9 h 59 m)

This course expands on the basics and covers more intermediate information on every key aspect of Angular, including component communication, creating directives, testing, and deployment.

Enjoy!

June 15, 2017

Property ‘x’ is private and only accessible within class ‘MyComponent’

Filed under: Angular @ 6:13 pm

 

The content of this post is based on Angular version >= 2.x unless explicitly stated otherwise.

As with any application framework, there are those issues that trip up many developers. Some of these are due to changes to the framework over time, others are just unexpected results from seemingly correct code. I’ve tried to capture some of the more common issues and their solutions and here is one of them.

Property ‘x’ is private and only accessible within class ‘MyComponent’.

This issue began with Angular 4 and the release of the CLI. The Angular CLI enables the Ahead of Time (AOT) compiler when you use the `ng build –prod` option. The Ahead of Time compiler compiles things a bit differently, hence the reason this issue only appears with AOT.

Original sample code:

import { Component } from '@angular/core';

@Component({
    selector: 'mh-root',
    template: `
        {{pageTitle}}
        <router-outlet></router-outlet>
     `
})
export class AppComponent {
    private pageTitle: string = 'InStep Movie Hunter';
}

Error: Property ‘pageTitle’ is private and only accessible within class ‘AppComponent’.

Issue: With AOT, private properties and methods in the class are only accessible within the class. To access the property or method in the template, the property or method must be declared public.

Updated sample code:

import { Component } from '@angular/core';

@Component({
    selector: 'mh-root',
    template: `
        {{pageTitle}}
        <router-outlet></router-outlet>
     `
})
export class AppComponent {
    public pageTitle: string = 'InStep Movie Hunter';
}

For more information: https://github.com/angular/angular-cli/issues/5623

Enjoy!

June 2, 2017

Passing Data to and Raising an Event from a Nested Component

Filed under: Angular @ 1:33 pm
Tags:

 

The content of this post is based on Angular version >= 2.x unless explicitly stated otherwise.

Let’s look at how to pass data to a nested component and raise an event from a nested component. But first, let’s define what a nested component is.

A nested component is an Angular component that provides some piece of the user interface and is embedded within the template of another Angular component. As shown in the image below, we created a component that displays a number as a set of stars. We can then nest this component in any other component that wants to use it, reusing it as needed. In this example, we nested it within the Product List component.

The code required to nest the component is shown below.

Thumbnail

In this example, the Star component is the component that will be used as a nested component. Notice it’s selector (ai-star). The Star component is nested in the Product List component’s template by using its selector as a directive.  The Product List component is then referred to as the parent component.

If we want to pass data from the parent component to the nested component, we define an @Input decorator on a property of the nested component. In this example, we define a rating property with the @Input decorator. This will be the rating value that we want to display as stars.

Then when we nest the component using the <ai-star> directive, we use data binding to bind the property decorated with @Input to a value from the parent component. This passes the data into the nested component.

In this example, we use property binding to bind the rating property to the product’s starRating property defined within the Product List component. That value then comes through to the @Input rating property in the start.component.ts.

But what if we want to raise an event from the nested component back to the parent? Let’s look at the flow for that:

Thumbnail

(1) The user clicks on the star component in the html. We use event binding to bind to a method in the Star Component.
(2) The specified method (onClick) is executed.
(3) The onClick method uses the emit method of an EventEmitter ((4) defined with an @Output decorator) to emit the desired event and passes ‘clicked!’ as the event parameter
(4) Each event to be emitted from the nested component must be defined with an @Output decorator. The event is created using the new keyword. The type of value passed to the event is defined as a generic parameter (<string> in this example)
(5) The parent component (Product List component in this example), uses event binding to bind to the event defined with the @Output decorator (4).
(6) When the event is emitted from the nested component (3), the onNotify method of the Product List component is called, passing in the event parameters (‘clicked!’ in this case)
(7) The onNotify method of the Product List component is executed and any code within that method is executed.

Enjoy!

May 5, 2017

Build a Simple Angular Service to Share Data

Filed under: Angular @ 2:20 pm
Tags: ,

The content of this post is based on Angular version >= 2.x unless explicitly stated otherwise.

I’ve seen some complex code on Stackoverflow and elsewhere recently where the developer was using Subject and Behavior Subject as part of an Observable solution to simply share data between unrelated components with Angular. In some cases, the Observable approach may be necessary. But in many cases, the solution can be much simpler without Observables.

For example, you may want to retrieve some user preferences and retain them for use by any component in the application. Or you may want to store some user-entered values, such as the selected currency, throughout the application.

Just build a simple service to service up that data to any component that injects the service.

image

The example in this post defines two unrelated components, Component A and Component B, that want to share data. But there could be any number of components that share this data.

First, we create a service that holds the data. Since a service is a singleton, all of the components that inject this service can share the data from this service:

import { Injectable } from '@angular/core';

@Injectable() 
export class DataService {
  serviceData: string; 
}

This particular service simply provides for sharing the service data. This data could be of any type, including an object. And there could be more code here, such as code to retrieve default values for this serviceData from a back-end server.

Each component then injects this service and uses its value:

import {Component} from '@angular/core'

import { DataService } from './data.service';

@Component({ 
 template: ` 
  <div> 
    <h2>Data from A: {{ data }} </h2> 
    <input [(ngModel)] = data /> 
    <br><br> 
    <a [routerLink]="['/b']">Go to B</a> 
  </div> 
  `, 
}) 
export class A {

  get data():string { 
    return this.dataService.serviceData; 
  } 
  set data(value: string) { 
    this.dataService.serviceData = value; 
  } 
  
  constructor(public dataService: DataService) { } 
}

There are two key bits of code here:

  • The dataService is injected into the constructor.
  • The property is defined as a getter/setter so that the current value is get/set from the service.

The second component is similar:

import {Component} from '@angular/core'

import { DataService } from './data.service';

@Component({ 
 template: ` 
  <div> 
    <h2>Data from B: {{ data }} </h2> 
    <input [(ngModel)] = 'data' /> 
    <br><br> 
    <a [routerLink]="['/a']">Go to A</a> 
  </div> 
  `, 
}) 
export class B { 
  get data():string { 
    return this.dataService.serviceData; 
  } 
  set data(value: string) { 
    this.dataService.serviceData = value; 
  } 
 
 constructor(public dataService: DataService) { } 
}

And for completeness, here are the app module and app component: (This was written using a Plunker, so the app module and app component are in the same file.)

import {Component, NgModule, VERSION} from '@angular/core'; 
import {BrowserModule} from '@angular/platform-browser'; 
import { RouterModule } from '@angular/router'; 
import { FormsModule } from '@angular/forms';

import { A } from './a.component'; 
import { B } from './b.component';

import { DataService } from './data.service';

@Component({ 
  selector: 'my-app', 
  template: ` 
  <div> 
    <h2>Hello {{name}}</h2> 
    <router-outlet></router-outlet> 
  </div> 
  `, 
}) 
export class App { 
  name:string; 
  constructor() { 
    this.name = `Angular! v${VERSION.full}` 
  } 
}

@NgModule({ 
  imports: [ 
    BrowserModule, 
    FormsModule, 
    RouterModule.forRoot([ 
      { path: '', redirectTo: 'a', pathMatch: 'full'}, 
      { path: 'a', component: A }, 
      { path: 'b', component: B } 
    ]) 
  ], 
  declarations: [ 
    App, 
    A, 
    B 
  ], 
  providers: [ DataService ], 
  bootstrap: [ App ] 
}) 
export class AppModule {}

In this particular case, since both components are routed to each other, this data could instead be passed to the components as a required, optional, or query routing parameter. But this solution works in cases where the components want to share data without necessarily passing it as part of routing.

You can find the plunker here: https://plnkr.co/edit/KT4JLmpcwGBM2xdZQeI9?p=preview

For more information on building services, check out my Pluralsight course: Angular 2: Getting Started

Enjoy!

March 31, 2017

“Angular Routing” Problem Solver

This blog post supports the sample code for the “Angular Routing” course on Pluralsight, identifying common issues along with their solutions.

The content of this post is based on Angular version >= 2.x unless explicitly stated otherwise.

The sample code can be found here: https://github.com/DeborahK/Angular-Routing. The folders in this repo include:

  • APM–Start: The starter files. Use this as a starting point to code along with the course. Updated to Angular v4.
  • APM–Final: The completed files. This code matches with the demos presented in the course. Updated to Angular v4.

Please post to the Discussion tab for the course if you experience an issue not found in this post. Thanks!

Here’s What Developers are Saying About the “Angular 2: Getting Started” Course

Filed under: Angular @ 2:05 pm
Tags: , ,

I had well over 1000 comments in the “Angular 2: Getting Started” discussion section (and another 1200+ for the Angular beta version of the course) before livefyre shut it down. Here is a sample of some of the great comments left by course viewers:

What Developers Are Saying

I always thought Angular was hard, but your way of explaining it makes it easy to understand and fun.

Extreme quality tutorial as always!

Many thanks for this really good introduction to Angular 2. It is so refreshing to come across a course that I can understand.

I just completed the course.  It has been one of the best training courses I’ve viewed.  I came into this course not liking the idea of a paradigm shift from Angular 1 to 2, but after viewing this course, I like 2 much better than 1.

Maybe the best tutorial I’ve seen of all programming tutorials. Love the way how you start and finish every module with checklists and revisitings. Angular 2 seems so easy to learn with you.

I have been using Angular 1.5 for over a year in a pure .net environment.  I was confused by the many Angular2 training options that did not fully explain Node and NPM.  Your course has opened my eyes.  Thank you for your explanations.  Great  job!  And great course!!

Thank you for the awesome course, Deborah! I really enjoyed it, learned a lot and chuckled quite a few times – especially at the "ng for nnngular" and you going "Yay!" every time something works – mainly because I do the exact same while I’m coding! :)  Really informative, easy to follow and information rich course – thank you!

Your course is one of the best, if not the best in Pluralsight and it has made learning Angular 2 fun and a lot less stressful.

Finished up the course yesterday and I must say it was a great learning experience.  Your organization and content is very well thought out (I’m sure I’ll revisit the checklists at the end of each module many times.)  Your delivery and pace were also excellent.

If you would be my teacher in the college ,then i would have given 100% attendance. Haha 😛 . You explain everything in a very good manner :).

This course is awesome. Impressive clarity.

Such a great course! Really made me understand the basic patterns of Angular 2, which was all very confusing for me before taking the course. Thank you!

Thank you for the SUPERP material on Angular 2. I have been following along your course and able to build my project.

Excellent method of teaching. Organized, lucid, coherent, structured.

Great course! How can anyone watch it just once?

Thanks for very useful course, I have completed the course and start doing my own angular 2 project for web application.

Just wanted to tell you how much I enjoyed your course, it’s given me a good foundation for angular2.

i really enjoy this course  it is great for beginners.

Thanks for a wonderful angular2 getting started course! It’s really helpful.

Thanks for a wonderful insight into Angular 2.

Outstanding. Teaching is an art which you have it in you. Thank you so much.

Thanks for the awesome course. I really enjoyed it.

your course is one of my favorite in the pluralsight. you are doing a great job!

This is a wonderful course, thank you.

thank you so much for your wonderful tutorial . I really love your teaching method . make them easy to understand.

Thank you for this great course. It is so detail and easy to follow.

Thank you for a great course. I loved the little bits of personality that you dropped in randomly. It makes a big difference for me.

A very good and detailed course; in-particular the recap and the end of each module and the really good slides. I am keeping those as reference diagrams on my wall.

Awesome course, Deborah is so clear and organized that even an idiot like me can understand.

This is a very exciting tutorial. I have learned so easy the Angular 2 base knowledge. Thank you very much.

Great course Deborah. Its become my reference point for all things Angular2.

Very helpful and structured tutorials! I wish I found out about these earlier.

Thanks Deborah for an excellent tour of Angular 2. Course content/speed is just too good. 5 stars from me.

Thank you for such a wonderful course. I was able to build a small app with your course guidance.

Great course, enjoying it very much, and particularly appreciate the way you present the material in a way where you deliberately encounter the same issues other "newbies" to Angular would encounter, and present the very logical resolutions to them. In that way I’m coming the appreciate the plumbing required so much more quickly than via some other resources I’ve been using.

Excellent content, your course coupled with John Papa’s course really helped me  get up and running with Angular 2. You are very thorough in your coverage, keep up the good work…

Fantastic, fantastic job — clear, comprehensive throughout, with clearly explained and shown examples.

Thanks Deborah for this wonderful and easily understandable course.Your course is designed and explained in such a wonderful way that after this course i do not require to search for another.Great job…….

You are an excellent tutor. I am loving the course and in spite of having no knowledge about angular JS version 1 i am getting a hold of angular 2. Thanks a ton.

Thank you for this fantastic course on Angular 2. Having never programmed in Angular 2 before, I found this course to be very helpful on the basic concepts of Angular 2. The amount of of time and detail spent on each topic contributed greatly to my understanding of programming in Angular 2. I very much look forward to your next course on Angular 2 – Angular 2: Reactive Forms.

Thanks Deborah for another excellent course.  I finished it without any problems.  Keep them coming 🙂

Super course I thoroughly enjoyed and went through it twice.

You are the most amazing tutor because:

  • You make everything so simple to understand.
  • Rather bluntly developing app and saying "What to do?", you take the time to say "Why to do it?".
  • You gave clear insight by going to every minute details. It is important to understand the basics.
  • You repeat and remind the concepts to make it familiar. It makes remembering them easy.
  • After completing this course, I now understand Architecture of Angular Application. So, I can develop manageable angular applications.

What Developers Are Requesting

Could you add additional lesson for paging service in future, in addition to firebase database with angular 2?

It would be great if you can throw some light on how to make use of font-awesome css & icons in the same project.

Kindly share the course or session on JIT & AOT

using npm behind a corporate proxy is a major hassle.  Is this addressed in a class somewhere?

I would like to have lectures on Security ( Angular2 application in banking domain ) and Unit testing.

A course on a web framework is incomplete IMO without coverage on working with forms data as intended by that framework.
Basic CRUD, even if with a mocked-up in-memory storage. [Covered in “Angular 2: Reactive Forms”]

Up next, NodeJS, React or React Native maybe?

Record an Ionic 2 course please!

It would be nice to see an advanced version for it. Something with Animations, Authorization, automatic build, testing. Things like that. [Covered in “Angular 2 Fundamentals”]

In your next course can you pl address some advanced topics like Forms, Security, Angular Material2. [Forms are covered in “Angular 2: Reactive Forms”]

January 9, 2017

Angular Route Ordering

Filed under: Angular @ 10:02 am
Tags: , ,

The content of this post is based on Angular version >= 2.x unless explicitly stated otherwise.

In Angular, the ordering of the routes in the route configuration matters. And when using feature and routing modules, it is easy to get the routes out of their needed sequence.

Basic Configuration

The following is an example Angular route configuration commonly defined in an app.module.ts file:

RouterModule.forRoot([

  { path: ‘welcome’, component: WelcomeComponent } ,

  { path: ‘products’, component: ProductListComponent },

  { path: ‘product/:id’, component: ProductDetailComponent }

  { path: ”, redirectTo: ‘welcome’, pathMatch: ‘full’},

  { path: ‘**’, redirectTo: ‘welcome’, pathMatch: ‘full’ }

])

The welcome route displays the template for the Welcome Component. (www.mysite.com/welcome)

The products route displays the template for the Product List Component. (www.mysite.com/products)

The product/:id route is a parameterized route. It displays the template for the Product Detail Component with the product defined by the id parameter. (www.mysite.com/product/42)

The empty path specifies a default route. This is the route that is matched when no path is specified. In this example, the default route redirects to the welcome route. (www.mysite.com)

The ** defines a wildcard path. This is the route that is matched when no other prior route is matched. It is often used for displaying a 404 (not found) page. But in this example, it redirects to the welcome route. (www.mysite.com/hello)

These routes are processed from top to bottom and the first match wins.

If we moved the wildcard path up to the top of the list like this:

RouterModule.forRoot([

  { path: ‘**’, redirectTo: ‘welcome’, pathMatch: ‘full’ }

  { path: ‘welcome’, component: WelcomeComponent } ,

  { path: ‘products’, component: ProductListComponent },

  { path: ‘product/:id’, component: ProductDetailComponent }

  { path: ”, redirectTo: ‘welcome’, pathMatch: ‘full’}

])

None of the other routes would ever be matched. The wildcard would always be picked first. So take care on the ordering of the routes in your route configurations.

Feature Modules

As an application gets larger, it is best to break out the features into their own feature modules. This makes the code easier to work with and maintain.

In the above example, we can refactor the product features out of the app.module.ts and add them to their own product.module.ts (ProductModule):

RouterModule.forChild([
    { path: ‘products’, component: ProductListComponent },
    { path: ‘product/:id’, component: ProductDetailComponent },
])

The resulting app.module.ts routes then look like this:

RouterModule.forRoot([

  { path: ‘welcome’, component: WelcomeComponent },

  { path: ”, redirectTo: ‘welcome’, pathMatch: ‘full’ },

  { path: ‘**’, redirectTo: ‘welcome’, pathMatch: ‘full’ }

]),

ProductModule

But wait! Isn’t our wildcard route now ahead of our product routes? How will our product routes ever be matched?

As it turns out, Angular handles this differently than we may expect. When the RouterModule.forRoute or RouterModule.forChild is used directly, it is processed last. All of the Angular modules (such as ProductModule) are processed first. So even though we define the wildcard route above ProductModule in this example, the routes defined in the RouterModule.forRoot() are moved to the end and processed last. So all of our routes still work as expected.

Routing Modules

In some cases, we may want to remove the routes from the app.module.ts and move them to their own module, such as app-routing.module.ts (AppRoutingModule):

RouterModule.forRoot([

{ path: ‘welcome’, component: WelcomeComponent },

{ path: ”, redirectTo: ‘welcome’, pathMatch: ‘full’},

{ path: ‘**’, redirectTo: ‘welcome’, pathMatch: ‘full’ }

])

If we then simply replace the routes in app.module.ts with the routing module like this:

AppRoutingModule,

ProductModule

Our product routes will stop working. Now that all of our routes are in modules, they are processed in their defined order. So the wildcard path in the AppRoutingModule will come *before* any of the routes defined in the ProductModule.

The app.module.ts modules must instead be defined in the appropriate order:

ProductModule,

AppRoutingModule

For more information on routing, check out my Pluralsight course: “Angular 2: Getting Started

For a code example, check out my github repo.

Enjoy!

October 18, 2016

Angular 2: Getting Started Course Update

This post identifies the changes to the “Angular 2: Getting Started” Pluralsight course from its first release on April 1st, 2016 to its update October 18th, 2016.

Angular 2 first appeared around June of 2015 as alpha.27. The first beta release of Angular 2 was December 12, 2015, a great time to start on an Angular 2 getting started course. The first release of the “Angular 2: Getting Started” course was on April 1st, 2016. The code examples for the course were valid through Angular beta.15. From that time until release, Angular underwent numerous changes … some, such as the new router and the introduction of Angular modules, were significant.

Angular 2.0.0 was released on September 14th, 2016, so that was a great time to update the “Angular 2: Getting Started” course. The changes made to each module of the course are outlined below.

If you have already watched the course and want to minimize which modules to re-watch, I’d suggest modules 2, 10, 11, and 12. That would provide the basics on Angular modules (NgModule) and the new router.

Module 1: Introduction

  • Introduced Angular modules.
  • Updated Course Outline.

Module 2: First Things First

  • Demoed how to get a more current version of npm.
  • Revised setup steps including the application’s root Angular module.
  • Updated discussion of AngularCli.
  • Updated demos for setting up Angular 2.
  • Enhanced discussion in “About Modules” regarding the difference between ES modules and Angular modules.

Module 3: Introduction to Components

  • Updated demos for new import statements (import from @angular/… instead of angular2/…)
  • A first cut of the app.component.ts is now included as part of the starter files. This changes some of the initial steps when building the app.component.ts file.
  • Added a callout to point out the use of the back tick (not quotes) for defining the inline template.
  • Bootstrapping process now includes the application’s root Angular module.
  • Additional checklist for resolving issues when coding along.

Module 4: Templates, Interpolation, and Directives

  • Updated demos for new import statements (import from @angular/… instead of angular2/…).
  • Added steps to update the application’s root Angular module (AppModule).
  • Added information on how an Angular module defines the boundary or context within which the component resolves its directives and dependencies.
  • Added a demonstration of a common Angular error and how to fix it.
  • Updated the ngFor syntax to match the released syntax using the let keyword instead of #.

Module 5: Data Binding & Pipes

  • Updated demos for new import statements (import from @angular/… instead of angular2/…).
  • Added information on adding FormsModule to an Angular module so the template can use ngModel for two-way binding.

Module 6: More on Components

  • Updated demos for new import statements (import from @angular/… instead of angular2/…).
  • Updated the code for the pipe transform to match the new transform signature.
  • Added a step to add the custom pipe to an Angular module.
  • Added a clip on using component-relative paths by setting module Ids.

Module 7: Building Nested Components

  • Updated demos for new import statements (import from @angular/… instead of angular2/…).
  • Added a step to add the nested component to an Angular module.

Module 8: Services and Dependency Injection

  • Updated demos for new import statements (import from @angular/… instead of angular2/…).
  • Mentioned how service can be registered in an Angular module.

Module 9: Retrieving Data Using Http

  • Updated demos for new import statements (import from @angular/… instead of angular2/…).
  • Enhanced discussion of Observables.
  • Enhanced the comparison between Promises and Observables.
  • Removed the setup clip since these setup steps are no longer valid.
  • Added a step to add Http to an Angular module.
  • Separated out the exception handling to its own clip.

Module 10: Navigation and Routing Basics

  • Divided this course module into two modules: 10 and 11.
  • Major modifications throughout to change to the current router.
  • Expanded on the details for how routing works.
  • Enhanced the discussion of Html 5 vs hash style routes.
  • Updated demos for new import statements (import from @angular/… instead of angular2/…).
  • Added steps to add the remaining components to an Angular module.

Module 11: Navigation and Routing Additional Techniques

  • This new module is the second half of the prior module.
  • Major modifications throughout to change to the current router.
  • Updated demos for new import statements (import from @angular/… instead of angular2/…).
  • Added a clip on route guards.

Module 12: Angular Modules

  • This new module is all about Angular modules. It covers:
    • The definition and purpose of an Angular module.
    • Understanding the Angular module metadata and its many rules.
    • Refactoring the sample application and creating a feature module.
    • Minimizing repetition by creating a shared module.
    • Reexamining the root application module (AppModule).
    • A look at building separate routing modules.
    • And summarizing options for your Angular module architecture.

Module 13: Angular 2 Setup Revisited

  • Updated the demos to the Angular 2 release setup files.
  • Added discussion of the systemjs.config.js file.
  • Added discussion of the bootstrapping process differences for the Just in Time (JiT) compiler vs the Ahead of Time (AoT) compiler.
  • Added discussion of the Angular CLI.

Module 14: Final Words

  • Minor wording changes.

Enjoy!

October 17, 2016

“Angular: Reactive Forms” Problem Solver

This blog post supports the sample code for the “Angular: Reactive Forms” course on Pluralsight, identifying common issues along with their solutions.

TBD

May 27, 2016

Angular 2: Filtering a List Using a Computed Property and ngModelChanged

Filed under: Angular @ 4:43 pm
Tags: , , ,

In my Pluralsight course: “Angular 2: Getting Started”, I demonstrate how to filter a list of products for display using a custom pipe. However, using a custom pipe does not provide easy access to the filtered result. For example, say you also want the count of the filtered list so you can display “5 of 125” or access other information about the list. In these cases, it is best to filter without using a custom pipe. This post shows you one technique: using a computed property and the ngModelChanged event.

Additional techniques for solving this problem are:

This post updates the code available from my github repo. So starting with that code:

1. Change the declarations adding a productsFiltered computed property. The productsFiltered property will contain the filtered list of products.

pageTitle: string = ‘Product List’;
imageWidth: number = 50;
imageMargin: number = 2;
showImage: boolean = false;
listFilter: string;
errorMessage: string;
products: IProduct[];
productsFiltered: IProduct[];

2. Add an onFilterChanged event handler that executes each time the filter input box is changed:

onFilterChanged(filter: any) { 
    this.listFilter = filter;
    filter = filter ? filter.toLocaleLowerCase() : null;
    this.productsFiltered = filter ? this.products.filter((product: IProduct) =>
        product.productName.toLocaleLowerCase().indexOf(filter) !== -1) : this.products;
}

This code sets the listFilter property and re-filters the list every time the listFilter input box changes. (Recall that we’ve removed the two-way binding, so we need to manually set the listFilter property.)

This is not enough, however. When the page is first displayed the product array is empty so nothing appears on the page. We need to initialize the computed property when the product data is retrieved.

3. One way to set the computed property when the data is retrieved is to modify the ngOnInit() method as follows:

ngOnInit(): void {
       this._productService.getProducts()
                 .subscribe(
                   products => this.onProductsRetrieved(products),
                   error =>  this.errorMessage = <any>error);
}

When the products are retrieved, this code calls the onProductsRetrieved method.

4. Create the new onProductsRetrieved method as follows:

onProductsRetrieved(products: IProduct[]) {
     this.products = products;
    this.productsFiltered = products;

}

This code sets the retrieved products to our product property and initializes our computed property.

5. The only other changes are to the template:

<input type=’text’ (ngModelChange)=’onFilterChanged($event)’ [ngModel]=’listFilter’ />

This separates the two-way binding into its one-way property binding with the [ngModel] and the one-way event binding with (ngModelChange). Whenever the model changes, the onFilterChanged method is called.

6. And change the template binding:

<tr *ngFor=’let product of productsFiltered‘>

This now binds to the productsFiltered property and no longer needs the pipe. We can then also bind other elements to this property or this property’s count method as needed.

Enjoy!

Next Page »

© 2017 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs