2016-11-16 3 views
3

Я пытаюсь использовать *ngFor в своем коде. По некоторым причинам, я получаю сообщение об ошибке при запуске приложения:Невозможно связать с * ngFor error

Я тройной проверил все имена свойств хороши, и нет никаких директив в этом случае. Я настроил приложение с помощью инструкций quickstart с угловым указателем.

Это мой 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 { } 
+0

Ваш вопрос не включает код, как вы используете 'NgFor' –

+0

Просьба указать код, показывающий, как вы используете' ngFor' и ваш класс '@ NgModule'. – Brad

+0

Привет, ты прав. Я добавил код –

ответ

2

Вам нужно добавить BrowserModule (если компонент находится в AppModule), иначе CommonModule к imports: [] вашего @NgModule()

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

Привет, спасибо за очень быстрый ответ. BrowserModule фактически уже импортирован. –

+0

В любом случае вам нужно импортировать 'CommonModule' в каждый модуль, который использует стандартные директивы. –

+0

'BrowseeModule' включает' CommonModule', поэтому для 'AppModule' первого достаточно. –

Смежные вопросы