2017-07-14 145 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>