gpt4 book ai didi

Angular 2 *ngFor 不绑定(bind)到字符串数组

转载 作者:太空狗 更新时间:2023-10-29 19:36:11 25 4
gpt4 key购买 nike

Angular 2 文档似乎是正确的。当使用 *ngFor="let hero of heroes"

在 app.html 中调用时,似乎无法识别位于 app.component.ts 中的我的字符串数组

只是尝试运行一个教程,非常感谢所有帮助!

应用程序组件.ts

import {
Component,
Attribute
} from 'angular2/core';
import {FORM_DIRECTIVES, CORE_DIRECTIVES} from 'angular2/common'



@Component({
selector: 'my-app',
templateUrl: 'app/app.html',
directives: [FORM_DIRECTIVES, CORE_DIRECTIVES]
})
export class AppComponent {
private newSearchTerm: string;
private channels: String[];
heroes = ['Windstorm', 'Bombasto', 'Magneta', 'Tornado'];

constructor() {
this.channels = [];
}

public newSubscription() {
this.channels.push(this.newSearchTerm);
this.newSearchTerm = '';
}
}

应用.html

<div id="app">
<div id="search-form">
<form (ngSubmit)="newSubscription()">
<input [(ngModel)]="newSearchTerm" placeholder="JavaScript" />
<button>Search</button>
</form>
</div>
<ul id="channels-list">
<li *ngFor="let hero of heroes">
{{ hero }}
</li>
</ul>
</div>

最佳答案

你可以这样使用:

heroes : String[]= ['Windstorm', 'Bombasto', 'Magneta', 'Tornado'];

关于Angular 2 *ngFor 不绑定(bind)到字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38086187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com