Angular 2 * ng不绑定到String数组

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

Angular 2文档似乎是正确的。使用* ngFor =“英雄英雄”在app.html中调用时,似乎无法识别位于app.component.ts中的我的String数组

只是尝试通过教程所有的帮助非常感谢!

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>
angular angular2-template ngfor
1个回答
-1
投票

你可以像这样使用:

英雄:String [] = ['Windstorm','Bombast','Magnetic','Tornado'];

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