英文:
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
<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>
Component TS file
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(){
}
}
答案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 '@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] // 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, '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() {}
}
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
<th>{{ todo.targetDate | date }}</th>
hope it's helpful..
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论