2017-11-28 10 views
0

私は$httpサービスを自分のフィルタに使用します。今すぐ次のエラーが発生しますES6クラスフィルタにサービスを挿入する

angular.js:12477 TypeError: Cannot read property 'get' of undefined 
    at factory (next-schedule-occurrence.filter.js:32) 
... 

$httpを正しく挿入するにはどうすればよいですか?

HTMLテンプレート:

<p>Next execution: {{watcher._source.trigger.schedule.later | nextScheduleOccurrence}}</p> 

フィルターコード:

/* global angular */ 
import moment from 'moment'; 
import later from 'later'; 

/* 
* Get the next occurence of from the 'later' lib text schedule 
*/ 
class NextScheduleOccurrence { 

    /* 
    * @param {string} schedule in English for the 'later' text parser 
    */ 
    constructor(schedule) { 
    this.schedule = schedule; 
    } 

    /* 
    * @return {string} the future occurrence for the schedule 
    */ 
    next(config) { 
    if (config.es.watcher.schedule_timezone === 'local') { 
     later.date.localTime(); 
    } 
    return moment(later.schedule(later.parse.text(this.schedule)).next()).format('D/M/YYYY H:m:s'); 
    } 

    /* 
    * @param {string} schedule in English for the 'later' text parser 
    */ 
    static factory(schedule, $http) { 
    const filter = new NextScheduleOccurrence(schedule); 
    return $http.get('../api/sentinl/config').then((config) => { 
     return filter.next(config); 
    }); 
    } 
} 

NextScheduleOccurrence.factory.$inject = ['schedule', '$http']; 
export default angular.module('nextScheduleOccurrence', []) 
.filter('nextScheduleOccurrence',() => NextScheduleOccurrence.factory); 

UPDATE

私はフィルタの代わりに、クラスの機能を使用する場合、私は同じエラーを取得します。

const NextScheduleOccurrence = function (schedule, $http) { 
    return $http.get('../api/config').then((config) => { 
    if (config.es.watcher.schedule_timezone === 'local') { 
     later.date.localTime(); 
    } 
    return moment(later.schedule(later.parse.text(schedule)).next()).format('D/M/YYYY H:m:s'); 
    }); 
}; 

NextScheduleOccurrence.$inject = ['schedule', '$http']; 
export default angular.module('nextScheduleOccurrence', []).filter('nextScheduleOccurrence',() => NextScheduleOccurrence); 
+1

のような機能を返すべきだと思います。 (2) 'this。$ http.get(...)'を使用する –

+0

@AlekseySolovey私は同じエラーを受け取ります – trex

+0

あなたは関数自体にパラメータとして渡す必要はありません、それは公共の変数ですあるクラスの中で、コンストラクタで宣言した 'this。$ http'を使用してください –

答えて

0

私はあなたのファクトリメソッドは、(1) `コンストラクタ(スケジュール、$ HTTP){この$ HTTP = $ HTTPこの

static factory($http) { 
    return function(schedule) { 
    const filter = new NextScheduleOccurrence(schedule); 
    return $http.get('../api/sentinl/config').then((config) => { 
     return filter.next(config); 
    }); 
    } 
} 
関連する問題