【问题标题】:angular 2, ngFor not working with error in console角度 2,ngFor 无法在控制台中出现错误
【发布时间】:2016-10-02 15:55:45
【问题描述】:

我正在尝试学习教程。这是一个显示课程的简单页面。我在控制台中遇到错误。 Angular 2 依赖是“angular2”:“2.0.0-beta.7”。我试过用“#”代替“让”。

无法绑定到“ngFor”,因为它不是已知的原生属性 (“{{标题}} ]*ngFor="让课程为课程"> {{课程}}

模块 course.components.ts

import {Component} from 'angular2/core';
import {CoursesService} from './courses.service';

@Component({    selector:'courses',
    template:`<h1>Courses</h1>
          {{title}}
          <ul>
             <li *ngFor="let course for courses">
          {{course}}
         </li>
          </ul>
          `,
        providers:[CoursesService]
    })
export class CoursesComponent
{
   title="The title of courses page";
   courses;

   constructor(coursesService:CoursesService)
   {
     this.courses = coursesService.getCourses();
   }
} 

Module course.service.ts
export class CoursesService
{
   getCourses():string[]
   {
     return ["Course1","Course2","Course3"];
   }
}

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    你错过了课程前的for

    <li *ngFor="let course of courses">
    

    并正确导入您的服务文件。

    我还为您创建了一个 plunker:http://plnkr.co/edit/ldpRkJLR89e6aF4McdQD?p=preview

    希望对您有所帮助!

    【讨论】:

    • 你的意思是我在课程前面使用了“for”而不是“of”。谢谢.. 成功了。
    【解决方案2】:

    Angular 2 几周前发布了最终版本,您应该更新到此最终版本以了解更改并注意在 2.0.0.beta.7 中 # 尚未弃用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多