无法绑定到* ngFor错误 [英] Can't bind to *ngFor error

查看:87
本文介绍了无法绑定到* ngFor错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在代码中使用*ngFor.出于某种原因,我在运行应用程序时收到此错误消息:

I'm trying to use *ngFor in my code. For some reason, I'm getting this error message when running the app:

我已经三遍检查了所有属性名称是否正确,在这种情况下没有任何指令.我使用angular.io快速入门说明配置了该应用.

I've triple checked all property names are good, and there aren't any directives in this case. I configured the app with the angular.io quickstart instructions.

这是我的tsconfig:

This is my tsconfig:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false

  }
}

这是我的package.json:

This is my package.json:

{
  "name": "angular-quickstart",
  "version": "1.0.0",
  "scripts": {
    "start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
    "lite": "lite-server",
    "tsc": "tsc",
    "tsc:w": "tsc -w"
  },
  "licenses": [
    {
      "type": "MIT",
      "url": "https://github.com/angular/angular.io/blob/master/LICENSE"
    }
  ],
  "dependencies": {
    "@angular/common": "~2.1.1",
    "@angular/compiler": "~2.1.1",
    "@angular/core": "~2.1.1",
    "@angular/forms": "~2.1.1",
    "@angular/http": "~2.1.1",
    "@angular/platform-browser": "~2.1.1",
    "@angular/platform-browser-dynamic": "~2.1.1",
    "@angular/router": "~3.1.1",
    "@angular/upgrade": "~2.1.1",
    "angular-in-memory-web-api": "~0.1.13",
    "core-js": "^2.4.1",
    "reflect-metadata": "^0.1.8",
    "rxjs": "5.0.0-beta.12",
    "systemjs": "0.19.39",
    "zone.js": "^0.6.25"
  },
  "devDependencies": {
    "@types/core-js": "^0.9.34",
    "@types/node": "^6.0.45",
    "concurrently": "^3.0.0",
    "lite-server": "^2.2.2",
    "typescript": "^2.0.3",
    "typings": "^2.0.0"
  }
}

这是我的systemjs.config:

This is my systemjs.config:

/**
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
  System.config({
    paths: {
      // paths serve as alias
      'npm:': 'node_modules/'
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      app: 'app',
      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
      '@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',
      // other libraries
      'rxjs':                      'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        main: './main.js',
        defaultExtension: 'js'
      },
      rxjs: {
        defaultExtension: 'js'
      }
    }
  });
})(this);

这是我使用ngFor的代码:

This is the code where i'm using ngFor:

import {Component} from "@angular/core";

@Component({
    selector: 'semester-component',
    template: `
                <div class="semester-div">
                  <ul>
                    <li *ngFor="let subject of subjectsNames">
                      <span>
                       <input #subjectName type="text" />
                       <input id = "subjectGrade" type = "number"/>
                       <input id = "subjectWeight" type = "number"/>
                      </span>
                    </li>
                  </ul>
                  <br>
                  <button (click)="addSubject(subjectName.value)">add</button>
                  <br>
                </div>
`,
})

export class semesterComponent {
    subjectsNames = ["math"];

    addSubject(subjectName: string) {
     if (subjectName) {
     this.subjectsNames.push(subjectName);
     this.subjectsNames.slice();
     console.log(subjectName);
     console.log(this.subjectsNames);
     }

     };

这是模块文件:

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import {AppComponent} from "./app.component";
import {semesterModule} from './semester/semester.module';

@NgModule({
  imports:      [ BrowserModule, semesterModule],
  declarations: [AppComponent],
  bootstrap: [AppComponent]
})
export class AppModule { }

推荐答案

您需要添加BrowserModule(如果组件位于AppModule中),否则需要将CommonModule添加到@NgModule()imports: []

You need to add BrowserModule (if the component is in AppModule) otherwise CommonModule to imports: [] of your @NgModule()

@NgModule(
  imports: [BrowserModule, /* other imports */],
  ...
)

这篇关于无法绑定到* ngFor错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆