Angular日期管道在HTML中未正常工作。

huangapple go评论72阅读模式
英文:

Angular date pipe not working properly in HTML

问题

Angular管道不起作用

我正在尝试使用Angular的日期管道。出于某种原因,似乎无法使其工作。我尝试了一些方法,如在通过管道传递之前将日期更改为字符串,或者将管道放在HTML中的其他位置,但似乎不起作用。非常感谢任何建议(:

组件HTML文件

<h1>My todos</h1>

<table border="1">
  <caption>Fun times ahead</caption>
  <caption>{{testDate}}</caption>
  <caption>{{testDate2 | date}}</caption>
  <thead>
    <tr>
      <th>Description</th>
      <th>Target Completion Date</th>
      <th>Is it done?</th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let todo of todos">
      <th>{{todo.description}}</th>
      <th>{{todo.targetDate}}</th>
      <th *ngIf="todo.done">Yes</th>
      <th *ngIf="!todo.done">No</th>
    </tr>
  </tbody>
</table>

组件TS文件

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

export class Todo {
  constructor(public id: number, public description: string, public done: boolean, public targetDate: string) {

  }
}

@Component({
  selector: 'app-list-todos',
  templateUrl: './list-todos.component.html',
  styleUrls: ['./list-todos.component.css']
})
export class ListTodosComponent implements OnInit {

  testDate: string = new Date(2010, 1, 1).toDateString();
  testDate2: string = new Date(2010, 1, 2).toDateString();

  todos = [
    new Todo(1, 'ex1', true, new Date().toDateString()),
    new Todo(2, 'ex2', false, new Date().toDateString()),
    new Todo(3, 'ex3', false, new Date().toDateString()),
    new Todo(4, 'ex4', false, new Date().toDateString()),
    new Todo(5, 'ex5', false, new Date().toDateString()),
    new Todo(6, 'ex6', false, new Date().toDateString()),
  ]

  constructor() {

  }

  ngOnInit() {

  }

}
英文:

Angular Pipe not working

I am trying to use Angular's date pipe. For some reason I can't seem to get it to work. I tried a couple things like changing the date to a string before passing it through the pipe or putting the pipe in other places in my HTML, but it doesn't seem to work. Any advice is greatly appreciated (:

Component HTML file

&lt;h1&gt;My todos&lt;/h1&gt;

&lt;table border=&quot;1&quot;&gt;
  &lt;caption&gt;Fun times ahead&lt;/caption&gt;
  &lt;caption&gt;{{testDate}}&lt;/caption&gt;
  &lt;caption&gt;{{testDate2 | date}}&lt;/caption&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Description&lt;/th&gt;
      &lt;th&gt;Target Completion Date&lt;/th&gt;
      &lt;th&gt;Is it done?&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr *ngFor=&quot;let todo of todos&quot;&gt;
      &lt;th&gt;{{todo.description}}&lt;/th&gt;
      &lt;th&gt;{{todo.targetDate}}&lt;/th&gt;
      &lt;th *ngIf=&quot;todo.done&quot;&gt;Yes&lt;/th&gt;
      &lt;th *ngIf=&quot;!todo.done&quot;&gt;No&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

Component TS file

import { Component, OnInit } from &#39;@angular/core&#39;;

export class Todo{
  constructor(public id:number, public description:string, public done:boolean, public targetDate:String){

  }
}

@Component({
  selector: &#39;app-list-todos&#39;,
  templateUrl: &#39;./list-todos.component.html&#39;,
  styleUrls: [&#39;./list-todos.component.css&#39;]
})
export class ListTodosComponent implements OnInit{

  testDate : String = new Date(2010, 1, 1).toDateString()
  testDate2 : String = new Date(2010, 1, 2).toDateString()

  todos = [
    new Todo(1, &#39;ex1&#39;, true, new Date().toDateString()),
    new Todo(2, &#39;ex2&#39;, false, new Date().toDateString()),
    new Todo(3, &#39;ex3&#39;, false, new Date().toDateString()),
    new Todo(4, &#39;ex4&#39;, false, new Date().toDateString()),
    new Todo(5, &#39;ex5&#39;, false, new Date().toDateString()),
    new Todo(6, &#39;ex6&#39;, false, new Date().toDateString()),
  ]

  constructor() {

  }

  ngOnInit(){

  }

}

答案1

得分: 0

导入DatePipe模块并在组件的构造函数中注入DatePipe。
现在应该将Date管道应用于表中每个待办事项对象的targetDate属性,根据您的区域设置格式化日期。

import { Component, OnInit } from '@angular/core';
import { DatePipe } from '@angular/common';

export class Todo {
  constructor(
    public id: number,
    public description: string,
    public done: boolean,
    public targetDate: string
  ) {}
}

@Component({
  selector: 'app-list-todos',
  templateUrl: './list-todos.component.html',
  styleUrls: ['./list-todos.component.css'],
  providers: [DatePipe] // 将DatePipe添加为提供者
})
export class ListTodosComponent implements OnInit {
  testDate: string = new Date(2010, 1, 1).toDateString();
  testDate2: string = new Date(2010, 1, 2).toDateString();

  todos = [
    new Todo(1, 'ex1', true, new Date().toDateString()),
    new Todo(2, 'ex2', false, new Date().toDateString()),
    new Todo(3, 'ex3', false, new Date().toDateString()),
    new Todo(4, 'ex4', false, new Date().toDateString()),
    new Todo(5, 'ex5', false, new Date().toDateString()),
    new Todo(6, 'ex6', false, new Date().toDateString()),
  ];

  constructor(private datePipe: DatePipe) {}

  ngOnInit() {}
}

在模板中使用DatePipe格式化日期:
在您的模板中,不要直接访问待办事项对象的targetDate属性,而是使用datePipe来格式化日期。修改显示目标日期的行如下:

<th>{{ todo.targetDate | date }}</th>

希望对您有所帮助。

英文:

Import the DatePipe module and Inject the DatePipe in your component's constructor.
Date pipe should now be applied to the targetDate property of each todo object in the table, formatting the date according to your locale

import { Component, OnInit } from &#39;@angular/core&#39;;
import { DatePipe } from &#39;@angular/common&#39;;

export class Todo {
  constructor(
    public id: number,
    public description: string,
    public done: boolean,
    public targetDate: string
  ) {}
}

@Component({
  selector: &#39;app-list-todos&#39;,
  templateUrl: &#39;./list-todos.component.html&#39;,
  styleUrls: [&#39;./list-todos.component.css&#39;],
  providers: [DatePipe] // Add DatePipe as a provider
})
export class ListTodosComponent implements OnInit {
  testDate: string = new Date(2010, 1, 1).toDateString();
  testDate2: string = new Date(2010, 1, 2).toDateString();

  todos = [
    new Todo(1, &#39;ex1&#39;, true, new Date().toDateString()),
    new Todo(2, &#39;ex2&#39;, false, new Date().toDateString()),
    new Todo(3, &#39;ex3&#39;, false, new Date().toDateString()),
    new Todo(4, &#39;ex4&#39;, false, new Date().toDateString()),
    new Todo(5, &#39;ex5&#39;, false, new Date().toDateString()),
    new Todo(6, &#39;ex6&#39;, false, new Date().toDateString()),
  ];

  constructor(private datePipe: DatePipe) {}

  ngOnInit() {}
}

Format the dates using the DatePipe in your template:
In your template, instead of directly accessing the targetDate property of your todo object, use the datePipe to format the date. Modify the line where you display the target date as

HTML

&lt;th&gt;{{ todo.targetDate | date }}&lt;/th&gt;

hope it's helpful..

huangapple
  • 本文由 发表于 2023年6月1日 02:08:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376248.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定