【问题标题】:angular 2 ngIf Unexpected token #angular 2 ngIf 意外令牌#
【发布时间】:2017-12-19 18:11:24
【问题描述】:

大家好,我正在研究 angular2,现在我正在使用 *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/AppComponent.html@1:5 ("
<h1>Angular</h1>
<div [ERROR ->]*ngIf='courses.length > 0 ; then #coursesList else 
#noCourses'>
</div>
"): ng:///AppModule/AppComponent.html@1:5

我不明白为什么我要遵循不想使用复制粘贴的指南,因为我需要知道为什么会发生这种情况

【问题讨论】:

    标签: angular components angular-ng-if


    【解决方案1】:

    您需要删除coursesListnoCourses 前面的#。 # 仅用于 ng-template 标签。

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

    【讨论】:

      猜你喜欢
      • 2016-07-22
      • 1970-01-01
      • 2017-07-08
      • 2023-03-12
      • 2019-02-01
      • 2016-10-21
      • 1970-01-01
      • 1970-01-01
      • 2017-09-04
      相关资源
      最近更新 更多