【问题标题】:Angular 2 *ngFor not binding to String arrayAngular 2 *ngFor 未绑定到字符串数组
【发布时间】:2016-10-31 08:52:42
【问题描述】:

Angular 2 文档似乎是正确的。在 app.html 中使用 *ngFor="let hero of hero" 调用时,我的位于 app.component.ts 中的字符串数组似乎无法识别

非常感谢所有帮助!

app.component.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 = '';
  }
}

app.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>

【问题讨论】:

  • 它正在使用 rc3 版本更改,请参阅 here

标签: angular angular2-template ngfor


【解决方案1】:

你可以这样使用:

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

【讨论】:

    猜你喜欢
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 2017-08-27
    • 2017-03-11
    • 2020-05-07
    相关资源
    最近更新 更多