0

There is a great pipe posted on here.

I'm trying to make it work with Ionic 2 RC version and getting these three errors:

  1. Type 'any' is not a constructor function type
  2. Cannot find name 'ChangeDetectorRef'. Parameter 'ref' of constructor from exported class has or is using private name 'ChangeDetectorRef'.
  3. Property 'interval' does not exist on type 'typeof Observable'

    import {Pipe, ChangeDetectorRef} from 'angular2/core';
    import {Observable} from 'rxjs/Observable';
    import {AsyncPipe} from 'angular2/common';
    
    
    @Pipe({
        name: 'messageTime',
        pure: false
    })
    export class MessageTimePipe extends AsyncPipe {  //Type 'any' is not a constructor function type
        value:Date;
        timer:Observable<string>;
    
    constructor(ref:ChangeDetectorRef) {  //Cannot find name 'ChangeDetectorRef'. Parameter 'ref' of constructor from exported class has or is using private name 'ChangeDetectorRef'.
        super(ref);
    }
    
    transform(obj:any, args?:any[]):any {
        if (obj instanceof Date) {
            this.value = obj;
    
            if(!this.timer) {
                    this.timer = this.getObservable();
            }
    
            return super.transform(this.timer, args);
        }
    
        return super.transform(obj, args);
    }
    
    private getObservable() {
        return Observable.interval(1000).startWith(0).map( () => {  //Property 'interval' does not exist on type 'typeof Observable'
            var result:string;
            // current time
            let now = new Date().getTime();
    
            // time since message was sent in seconds
            let delta = (now - this.value.getTime()) / 1000;
    
            // format string
            if (delta < 10) {
                result = 'jetzt';
            }
            else if (delta < 60) { 
                // sent in last minute
                result = 'vor ' + Math.floor(delta) + ' Sekunden';
            }
            else if (delta < 3600) { 
                // sent in last hour
                result = 'vor ' + Math.floor(delta / 60) + ' Minuten';
            }
            else if (delta < 86400) { 
                // sent on last day
                result = 'vor ' + Math.floor(delta / 3600) + ' Stunden';
            }
            else { 
                // sent more than one day ago
                result = 'vor ' + Math.floor(delta / 86400) + ' Tagen';
            }
            return result;
        });
    };
    

    }

Any idea on how to solve this?

Edit: I was able to take out error 3/ by using import {Observable} from 'rxjs/Rx';

Community
  • 1
  • 1
Dee
  • 909
  • 3
  • 10
  • 18

2 Answers2

0

Use angular 2 moment why do you needs to create something that already exists?

khocef
  • 105
  • 11
  • Ionic 2 is in RC version and with the new build system with rollup, it's hard to get third party library working in the project. And if I can avoid an external dependency with an angular pipe, it would make things much easier. – Dee Oct 30 '16 at 14:29
0

For rxjs 6.*

import { interval } from "rxjs";
const observable = interval(1000);
Mário Kapusta
  • 488
  • 7
  • 19