I have noticed three problems.
- In your .HTML, your *ngFor loop is wrong.
You're iterating over this.myDestination.name
, which doesn't exist on the array "myDestination" and is not a property of the Array class. Remove the '.name' from your *ngFor
loop to iterate successfully. In the comments, you mentioned other errors, this could be related your second line in the loop where you are accessing destination.city.name
which is accessing .name
of undefined
.
Based on your service.TS file, your array would have the structure [{BookName: string}]
. To visualise it, you can write it out:
const myDestination = [{"BookName" : "No Record Found"}]
, which if you play around with your IDE in the TS, it will help you with understanding that neither .name
exists on the array as a property, and neither is it set as a variable on any of the items in the array:
And neither does it exist on iterated items:
Which leads into the second problem I noticed:
2. In your .HTML, you're asking Angular to print out the value destination.city.name
, which doesn't exist, either:
- In your home.component.ts, at declaration you're casting myDestination to type
any
, which confuses the IDE to guide you that it is indeed an array. Instead, write this: myDestination: Array<any> = []
. Read more about casting here and Type Assertion here.
When you write your code out like this, it will help you understand that Angular is only as capable as the JavaScript (Typescript) you give it.
Have a look in the official documentation to learn more.
Some highly opinionated jargon:
- Naming of arrays should be plural - myDestinations
- Don't use type
any
unless absolutely necessary.
- Using plugins like Angular Language Service (or alike for your IDE) will assist you to detect these problems sooner.
- DebounceTime should be used before calling a service (or at least, before calling the API) and not on the response.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…