【问题标题】:Can't bind to *ngFor error [duplicate]无法绑定到 *ngFor 错误 [重复]
【发布时间】:2017-03-31 06:47:06
【问题描述】:

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

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

这是我的 tsconfig:

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

  }
}

这是我的 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:

/**
 * 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 的代码:

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 { }

【问题讨论】:

  • 你的问题不包括你如何使用的代码NgFor
  • 请发布代码,显示您如何使用ngFor 和您的@NgModule 课程。
  • 嗨,你是对的。我已经添加了代码
  • 添加的代码看起来不错。 NgModulesemesterComponent 所在的位置有何相似之处?
  • semesterModule 长什么样子?

标签: angular typescript ngfor


【解决方案1】:

你需要添加BrowserModule(如果组件在AppModule中)否则CommonModuleimports: []@NgModule()

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

【讨论】:

  • 您好,感谢您的快速回复。 BrowserModule 实际上已经被导入了。
  • 无论如何你需要将CommonModule 导入到每个使用标准指令的模块中。
  • BrowseeModule 包括CommonModule,所以对于AppModule,前者就足够了。
  • 解决了你的问题吗?
  • 感谢@GünterZöchbauer 我遇到了同样的问题并解决了导入 CommonModule 的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-28
  • 2017-02-07
  • 1970-01-01
相关资源
最近更新 更多