相关疑难解决方法(0)

Angular 2"time ago"pipe

我正在尝试为我的Angular 2应用程序创建一个'time ago'管道.

它应该将日期转换为字符串,例如"5分钟前"或"60秒前".它到目前为止工作得很好,但在第一次计算后它没有更新.如果给定日期是例如5秒前,它显示'5秒前',但在此之后永远不会改变.

我已经尝试将管道的"纯"值设置为false但这没有帮助.

这是我的代码:

import {Pipe, PipeTransform} from 'angular2/core';

@Pipe({
  name: 'messageTime',
  pure: false
})
export class MessageTimePipe implements PipeTransform {
  transform(value: Date, []): string {
    var result: string;

    // current time
    let now = new Date().getTime();

    // time since message was sent in seconds
    let delta = (now - value.getTime()) / 1000;

    // format string
    if (delta < 10) {
      result = 'jetzt';
    } else if (delta < 60) { // sent in last minute
      result = …
Run Code Online (Sandbox Code Playgroud)

date pipe angular

29
推荐指数
2
解决办法
2万
查看次数

模板解析:找不到管道

我收到错误消息:

模板解析错误:找不到管道“ amDateFormat”

这是我的 app.module.ts

import { NgModule } from '@angular/core';
...
import { MomentModule } from 'angular2-moment';
...

@NgModule({
  declarations: [
    MyApp
  ],
  imports: [
    ...
    MomentModule,
    ...
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp
  ],
  providers: [
    ...
  ]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

然后在service-booking-details.ts执行以下操作:

import { Component} from '@angular/core';
import { IonicPage, NavController, NavParams} from 'ionic-angular';
import { MomentModule } from 'angular2-moment';
...    

@IonicPage()
@Component({
  selector: 'page-item-detail',
  templateUrl: 'service-booking-detail.html'
})
export class ServiceBookingDetailPage {
  service: …
Run Code Online (Sandbox Code Playgroud)

ionic2 angular2-moment angular

2
推荐指数
1
解决办法
2952
查看次数

标签 统计

angular ×2

angular2-moment ×1

date ×1

ionic2 ×1

pipe ×1