Angular2 - "无法绑定到' ngSwitchWhen'因为它不是“模板”的已知属性。"。"

时间:2017-03-10 13:05:07

标签: angular typescript ng-modules

我收到以下错误"无法绑定到' ngSwitchWhen'因为它不是“模板”的已知属性。"。"我已经阅读了建议添加的不同主题

  

从' @ angular / common'

导入{CommonModule}

并添加" CommonModule"进入@NgModel的导入部分,我做了,但这并没有解决问题。我无法弄清楚我做错了什么,有什么帮助来解决这个问题?

这是" app.component.ts"

的代码
import { Component } from '@angular/core'

@Component({
  selector: 'app-root',
  template: `
     <ul class="nav nav-pills">
        <li [class.active]="viewMode == 'map'"><a (click)="viewMode = 'map'">Map View</a></li>
        <li [class.active]="viewMode == 'list'"><a (click)="viewMode = 'list'">List View</a></li>
     </ul>
     <div [ngSwitch]="viewMode">
     <template [ngSwitchWhen]="'map'" ngSwitchDefault>Map View Content></template>
     <template [ngSwitchWhen]="'list'">List View Content</template>
     </div>
    `
})

export class AppComponent {
  viewMode = 'map';
} 

这是&#34; app.module.ts&#34;

的代码
import { BrowserModule } from '@angular/platform-browser';
import { NgModule, ApplicationRef } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { CommonModule } from '@angular/common';

import { AppComponent } from './app.component';
import { FavoriteComponent } from './favorite.component';
import { HeartComponent } from './heart.component';
import { VoteComponent } from './vote.component';
import { TweetComponent } from './tweet.component';

@NgModule({
  declarations: [
    AppComponent,
    FavoriteComponent,
    HeartComponent,
    VoteComponent,
    TweetComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    CommonModule
  ],
  providers: [],
  entryComponents: [AppComponent],
  bootstrap: [AppComponent]
})

export class AppModule {

}

1 个答案:

答案 0 :(得分:12)