2017-07-14 111 views
3

大家好我是研究angular2現在IM workind與*ngIf和我有這樣的代碼角2 ngIf意外標記#

<div *ngIf='courses.length > 0 ; then #coursesList else #noCourses'> 
</div> 
<ng-template #coursesList> 
    <h1>List of courses</h1> 
</ng-template> 
<ng-template #noCourses> 
    <h2>No courses yet</h2> 
</ng-template> 

而這是我component.ts

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

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'], 
}) 
export class AppComponent { 
    title = 'app hello'; 

    courses=[]; 
} 

我的問題,當我運行網站我有這個錯誤:

Uncaught Error: Template parse errors: 
Parser Error: Unexpected token # at column 27 in [courses.length > 0 ; then 
#coursesList else #noCourses] in ng:///AppModule/[email protected]:5 (" 
<h1>Angular</h1> 
<div [ERROR ->]*ngIf='courses.length > 0 ; then #coursesList else 
#noCourses'> 
</div> 
"): ng:///AppModule/[email protected]:5 

我不明白爲什麼我fo在不想使用複製粘貼的指導中,因爲我需要知道爲什麼會發生這種情況

回答

3

您需要刪除coursesListnoCourses前面的#號。 #僅用於ng-template標籤。

<div *ngIf='courses.length > 0 ; then coursesList else noCourses'> 
</div>