【问题标题】:ng Template can not repeat the data but HTML element can display itng 模板不能重复数据但 HTML 元素可以显示它
【发布时间】:2019-02-07 19:34:56
【问题描述】:

我想使用 ng 模板显示 json 数据但无法显示数据。但是当我使用 div 或 span 之类的 HTML 元素时,它可以显示它

json格式

常量 arr = [ { “日期”:1, “颜色”:“正常” }, { “日期”:2, “红色” }, { “日期”:3, “颜色”:“正常” } ]

在 TS 中;

dateArray: any;

public getJSON() {
     return this.http.get("../assets/eventcalendar.json")
        .pipe(map(res => res.json()));
  }

  getCalendar(){
    this.getJSON()
    .subscribe((event:any)=>{
        let getEvent = JSON.stringify(event);
        this.dateArray = JSON.parse(getEvent);
        console.log('this.dateArray', this.dateArray);
    })

  }

在 HTML 中:(此模板无法显示 json 数据)

<p-calendar (onSelect)="select($event)" [inline]="true" selectionMode="multiple" readonlyInput="true">
  <ng-template pTemplate="body" let-item="dateArray">
    <span 
      [ngClass]="item.color">
      {{item.tgl}}
    </span>
  </ng-template>
</p-calendar>

在 HTML 中:(此模板可以显示 json 数据但日历 UI 不好)

<p-calendar (onSelect)="select($event)" [inline]="true" selectionMode="multiple" readonlyInput="true">
  <ng-template pTemplate="body">
    <span *ngFor="let item of dateArray"
      [ngClass]="item.color">
      {{item.tgl}}
    </span>
  </ng-template>
</p-calendar>

我使用primeng calendar作为日历并使用angular 7 latest 谢谢

【问题讨论】:

    标签: typescript primeng angular7


    【解决方案1】:

    @Alkim Al,试试这个

    将其更改为

    <ng-template pTemplate="body" let-item="dateArray">
    

    下线

    <ng-template ngFor let-item [ngForOf]="dateArray">
    

    这可能会解决您的问题。欲了解更多信息check here once

    【讨论】:

    • 我已经尝试过了,但没有任何反应。
    • 如果我只使用 ng-template 而不是来自 primeng 的元素 p-calendar,则数据显示并运行良好。哪些primeng元素p-calendar不能用其他数据覆盖?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    • 2014-04-26
    • 1970-01-01
    • 2016-04-08
    相关资源
    最近更新 更多