Angular 5 Example - To be able to enter new item to empty list

"[Angular Example - Tour of Heroes: Part 6](https://angular.io/generated/live-examples/toh-pt6/stackblitz.html)" in the tutorial of Angular 5 has a problem to enter an item into the heros.component.  This article shows a way to solve it.

#The problem of heros.component
First, delete all items from the list of the heros.component.

![1802001_001.png](https://qiita-image-store.s3.amazonaws.com/0/128949/91df3065-daa4-eaad-0a71-40fee060d2eb.png)
####Figure 001•Empty list

Second, enter new item in the text field and press the [add] button.

![1802001_002.png](https://qiita-image-store.s3.amazonaws.com/0/128949/f333b699-359a-ffb6-d55f-be5e0d4e72b3.png)
####Figure 002•Entering new item

Then listed item has neither id nor name.  Browser console indicates the error message like below:

```
body: {error: "Collection 'heroes' id type is non-numeric or unknown. Can only generate numeric ids."}
```

![1802001_003.png](https://qiita-image-store.s3.amazonaws.com/0/128949/5df141bb-8f33-ec8f-f2c6-9f52cdddc1f4.png)
####Figure 003•Blank item is inserted into the list

When the array of data comes to be empty, nothing has the id property.  Then number for id does not seem to be generated and new item cannot be registered.

#Use HttpClient.put() method to add an item with id
The [`HttpClient.put()` method](https://angular.io/api/common/http/HttpClient#put) sets data to the specific id.  Revise the `HeroService.addHero()` method to call the HttpClient.put() method when the data array is empty.

```typescript
export class HeroService {

	// addHero(hero: Hero): Observable<Hero> {
	addHero(hero: Hero, numHeros: number): Observable<Hero> {
		let heroOvserbable;
		// return this.http.post<Hero>(this.herosUrl, hero, httpOptions)
		if (numHeros === 0) {
			hero.id = 11;
			heroOvserbable = this.http.put(this.herosUrl, hero, httpOptions)
		} else {
			heroOvserbable = this.http.post<Hero>(this.herosUrl, hero, httpOptions);
		}
		return heroOvserbable

	}

}
```

Since the HeroService class does not know the data array, the HerosComponent.add() method passes the Array.length property of data as an argument to call the HeroService.addHero().

```typescript
export class HerosComponent implements OnInit {

	add(name: string): void {

		// this.heroService.addHero({name} as Hero)
		this.heroService.addHero({name} as Hero, this.heros.length)

	}

}
```

Now new item can be entered even if the list is empty.  Revised example is "[Angular Example - Tour of Heroes: Part 6 revised](https://stackblitz.com/edit/angular-5-tutorial-example)".

#Overriding a method of Angular in-memory-web-api module
The reason why an id for new item is not generated when the list is empty might be based on the specification of the [Angular in-memory-web-api module](https://github.com/angular/in-memory-web-api#angular-in-memory-web-api).  [`BackendService.genIdDefault()` method](https://github.com/angular/in-memory-web-api/blob/f7e216d626207f5a553aa710463b5d06b39b8c43/src/in-mem/backend.service.ts#L402-L419) to provide the default id number calls [`BackendService.isCollectionIdNumeric()`](https://github.com/angular/in-memory-web-api/blob/f7e216d626207f5a553aa710463b5d06b39b8c43/src/in-mem/backend.service.ts#L509-L517) to make sure of id property in the collection.  Only if the property exists and is numeric, new number will be provided.

The number for new entry is generated with the [`BackendService.genId()` method](https://github.com/angular/in-memory-web-api/blob/f7e216d626207f5a553aa710463b5d06b39b8c43/src/in-mem/backend.service.ts#L386-L400) which can be overridden by the InMemoryDataService class (in-memory-data.service module).  This is another way to solve the problem.

The method below return the max number of ids plus one (+1) if they exists or the initial number (11) if it does not.

```typescript
export class InMemoryDataService implements InMemoryDbService {

	genId(heroines: Heroine[]): number {
		return heroines.length > 0 ? Math.max(...heroines.map(heroine => heroine.id)) + 1 : 11;
	}
}
```

See "[Tutorial example has a problem to enter new item into an empty list](https://github.com/angular/angular/issues/22120#issuecomment-364632192)" and example code "[Angular Example - Tour of Heroes: Part 6](https://stackblitz.com/edit/angular-8r8msa?file=src%2Fapp%2Fin-memory-data.service.ts)".