Angular2 @Input()未设置数组

问题描述 投票:0回答:1

试图将数组传递给@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类

@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输出的内容,因为您可以看到除了标记列表之外的所有输入属性都已设置

我可能在这里遗漏了一些东西,如果有什么让我知道的话。提前致谢。

angular angular2-template typescript2.0
1个回答
1
投票

如评论中所述,在将标签列表绑定到result.tagList组件的result.tags输入时,您输入了一个拼写错误(tagsList而不是searchresult)。

要解决此问题,您必须更改绑定到[tagList] = "result.tags"

© www.soinside.com 2019 - 2024. All rights reserved.