【问题标题】:Wikipedia Search bar error: "Can't bind to *ngFor" (Angular2 / HTML / Javascript)维基百科搜索栏错误:“无法绑定到 *ngFor”(Angular2 / HTML / Javascript)
【发布时间】:2016-05-12 20:28:15
【问题描述】:

我基本上是在尝试创建一个搜索栏,搜索数据然后显示该数据(通过基于单击按钮后搜索框输入的过滤器)(来自维基百科或只是本地假数据,它没有没关系)

此错误已在另一个问题中多次发布,但解决方案的上下文差异太大,无法帮助我。

这是错误的截图:

enter image description here

这是完整的项目(没有nodes_modules文件夹):

https://www.dropbox.com/s/kxq1qgfhh8huudz/FullProject.zip?dl=0

有错误的代码 (wiki.component.ts):

import { Component }        from 'angular2/core';
import { JSONP_PROVIDERS }  from 'angular2/http';
import { Observable }       from 'rxjs/Observable';
import { WikipediaService } from './wikipedia.service';
@Component({
  selector: 'Wikithing',
  template: `
    <h1>Wikipedia Demo</h1>
    <p><i>Fetches after each keystroke</i></p>
    <input #term (keyup)="search(term.value)"/>
    <ul>
      <li *ngFor="let item of items | async">{{item}}</li>
    </ul>
  `,
  providers:[JSONP_PROVIDERS, WikipediaService]
})
export class WikiComponent {
  constructor (private wikipediaService: WikipediaService) {}
  items: Observable<string[]>;
  search (term: string) {
    this.items = this.wikipediaService.search(term);
  }
}

文本格式错误:

Failed to load resource: the server responded with a status of 404 (Not Found)
angular2.dev.js:384 Angular 2 is running in the development mode. Call enableProdMode() to enable the production mode.
angular2.dev.js:23935 EXCEPTION: Error: Uncaught (in promise): Template parse errors:
Parser Error: Unexpected token | at column 25 in [ngFor let item of items | async] in WikiComponent@5:10 ("
    <input #term (keyup)="search(term.value)"/>
    <ul>
      <li [ERROR ->]*ngFor="let item of items | async">{{item}}</li>
    </ul>
  "): WikiComponent@5:10

【问题讨论】:

    标签: javascript html angular


    【解决方案1】:

    您需要使用#item 而不是let item,因为您已经安装了angular 2.0.0-beta.15。

    <li *ngFor="#item of items | async">{{item}}</li>
    

    另见https://github.com/angular/angular/blob/master/CHANGELOG.md#200-beta17-2016-04-28

    【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签