admin管理员组文章数量:1389772
I have a binding {{getCategoryName(post.categories[0])['name']}}
where I need to process the value post.categories[0]
returned in the *ngFor
loop with a method getCategoryByName()
in my ponent before finally binding it to the template.The method returns an Object
.
I keep getting the error Cannot read property 'name' of undefined
. I have tried getting the property using the ['name']
key instead of .name
but still get the error.
Any thoughts for a workaround?
<ion-item text-wrap class="news-item" *ngFor="let post of posts" (click)="viewStory(post)">
<div >
<p>{{getCategoryName(post.categories[0])['name']}}</p>
<h2>{{post.title.rendered}}</h2>
<p>By Fitz · {{post.date | fromNow}}</p>
</div>
</ion-item>
The method
getCategoryName(categoryId) {
return this.categories[this.categories.findIndex((el)=>{ return el.id == categoryId})];
}
Example ember of the Array
from which the Object
is returned by method above
[
{
"id": 33,
"count": 1,
"description": "",
"link": "/",
"name": "Apps",
"slug": "apps",
"taxonomy": "category",
"parent": 0,
"meta": [],
"_links": {
"self": [
{
"href": ""
}
],
"collection": [
{
"href": ""
}
],
"about": [
{
"href": ""
}
],
"wp:post_type": [
{
"href": ""
},
{
"href": ""
}
],
"curies": [
{
"name": "wp",
"href": "/{rel}",
"templated": true
}
]
}
},
]
I have a binding {{getCategoryName(post.categories[0])['name']}}
where I need to process the value post.categories[0]
returned in the *ngFor
loop with a method getCategoryByName()
in my ponent before finally binding it to the template.The method returns an Object
.
I keep getting the error Cannot read property 'name' of undefined
. I have tried getting the property using the ['name']
key instead of .name
but still get the error.
Any thoughts for a workaround?
<ion-item text-wrap class="news-item" *ngFor="let post of posts" (click)="viewStory(post)">
<div >
<p>{{getCategoryName(post.categories[0])['name']}}</p>
<h2>{{post.title.rendered}}</h2>
<p>By Fitz · {{post.date | fromNow}}</p>
</div>
</ion-item>
The method
getCategoryName(categoryId) {
return this.categories[this.categories.findIndex((el)=>{ return el.id == categoryId})];
}
Example ember of the Array
from which the Object
is returned by method above
[
{
"id": 33,
"count": 1,
"description": "",
"link": "http://XXXXXXXXXXXXX.co/category/apps/",
"name": "Apps",
"slug": "apps",
"taxonomy": "category",
"parent": 0,
"meta": [],
"_links": {
"self": [
{
"href": "http://XXXXXXXXXXXXX.co/wp-json/wp/v2/categories/33"
}
],
"collection": [
{
"href": "http://XXXXXXXXXXXXX.co/wp-json/wp/v2/categories"
}
],
"about": [
{
"href": "http://XXXXXXXXXXXXX.co/wp-json/wp/v2/taxonomies/category"
}
],
"wp:post_type": [
{
"href": "http://XXXXXXXXXXXXX.co/wp-json/wp/v2/posts?categories=33"
},
{
"href": "http://XXXXXXXXXXXXX.co/wp-json/wp/v2/jobs?categories=33"
}
],
"curies": [
{
"name": "wp",
"href": "https://api.w/{rel}",
"templated": true
}
]
}
},
]
Share
Improve this question
edited Nov 15, 2017 at 22:03
fitzmode
asked Nov 15, 2017 at 21:38
fitzmodefitzmode
1,0762 gold badges22 silver badges32 bronze badges
2
-
show the body of
getCategoryName
and the structure ofposts
array – El houcine bougarfaoui Commented Nov 15, 2017 at 21:44 - @BougarfaouiElhoucine I've edited to show the structure the object (Just one entry for brevity) and the method. – fitzmode Commented Nov 15, 2017 at 22:05
2 Answers
Reset to default 4You could simply do like this
{{getCategoryName(post.categories[0])?.name}}
?
is the safe navigation operator. It checks whether the variable is null or undefined so that our template won't try to select a property of something falsy.
More info: https://angular.io/guide/template-syntax#the-safe-navigation-operator----and-null-property-paths
In your getCategoryName method check if post.categories is undefined, and then return object {name : ''}:
if (post.categories === undefiend) {
return {name : ''}
}
or you could use in template:
{{(getCategoryName(post.categories[0]) || {name : ''})['name']}}
本文标签: javascriptUndefined properties in Angular expression bindingStack Overflow
版权声明:本文标题:javascript - Undefined properties in Angular expression binding - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744697783a2620384.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论