未设置 Angular2 @Input() 数组
Angular2 @Input() Array not being set
正在尝试将数组传递给 @Input() 参数,但它根本不显示。
<ng-container *ngIf = "searchResults != undefined">
<searchresult *ngFor = "let result of searchResults;"
[title] = 'result.title'
[authorName] = 'result.authorName'
[content] = 'result.content'
[tagList] = "result.tagList"
></searchresult>
</ng-container>
<p *ngIf = "searchResults == undefined">loading...</p>
然后在搜索结果中我有
<div class = "search-result">
<div class = 'result-top-section'>
<div class = 'author-profile'>
<img width = '70' height = '70' class = 'profile-icon' src= '/images/ogpfp.png'/>
<p>{{ authorName }}</p>
</div>
<div class = 'content'>
<h2>{{ title }}</h2>
<p>{{ content }}</p>
</div>
</div>
<div class = 'result-tag-row'>
<tag *ngFor = "let tag of tagList;" [tagName] = 'tag'></tag>
</div>
这是 SearchResultComponent class
@Component({
selector: 'searchresult',
templateUrl: './searchResult.component.html',
styleUrls: ['./searchResult.component.css']
})
export class SearchResultComponent implements ISearchResult{
@Input()
title: string;
@Input()
authorName: string;
@Input()
content: string;
@Input()
tagList: string[];
}
https://snag.gy/Nt4JIo.jpg
这是数组,您可以看到标签列表已填充
https://snag.gy/SIX0Gs.jpg
这是 html 输出的内容,正如您所看到的,每个输入 属性 都已设置,但标签列表
除外
我可能在这里遗漏了什么,如果有什么请告诉我。提前致谢。
如评论中所述,您在将标签列表绑定到 searchresult
组件的 tagsList
输入时输入错误(result.tagList
而不是 result.tags
) .
要解决此问题,您必须将绑定更改为 [tagList] = "result.tags"
。
正在尝试将数组传递给 @Input() 参数,但它根本不显示。
<ng-container *ngIf = "searchResults != undefined">
<searchresult *ngFor = "let result of searchResults;"
[title] = 'result.title'
[authorName] = 'result.authorName'
[content] = 'result.content'
[tagList] = "result.tagList"
></searchresult>
</ng-container>
<p *ngIf = "searchResults == undefined">loading...</p>
然后在搜索结果中我有
<div class = "search-result">
<div class = 'result-top-section'>
<div class = 'author-profile'>
<img width = '70' height = '70' class = 'profile-icon' src= '/images/ogpfp.png'/>
<p>{{ authorName }}</p>
</div>
<div class = 'content'>
<h2>{{ title }}</h2>
<p>{{ content }}</p>
</div>
</div>
<div class = 'result-tag-row'>
<tag *ngFor = "let tag of tagList;" [tagName] = 'tag'></tag>
</div>
这是 SearchResultComponent class
@Component({
selector: 'searchresult',
templateUrl: './searchResult.component.html',
styleUrls: ['./searchResult.component.css']
})
export class SearchResultComponent implements ISearchResult{
@Input()
title: string;
@Input()
authorName: string;
@Input()
content: string;
@Input()
tagList: string[];
}
https://snag.gy/Nt4JIo.jpg 这是数组,您可以看到标签列表已填充
https://snag.gy/SIX0Gs.jpg 这是 html 输出的内容,正如您所看到的,每个输入 属性 都已设置,但标签列表
除外我可能在这里遗漏了什么,如果有什么请告诉我。提前致谢。
如评论中所述,您在将标签列表绑定到 searchresult
组件的 tagsList
输入时输入错误(result.tagList
而不是 result.tags
) .
要解决此问题,您必须将绑定更改为 [tagList] = "result.tags"
。