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);
のような機能を返すべきだと思います。 (2) 'this。$ http.get(...)'を使用する –
@AlekseySolovey私は同じエラーを受け取ります – trex
あなたは関数自体にパラメータとして渡す必要はありません、それは公共の変数ですあるクラスの中で、コンストラクタで宣言した 'this。$ http'を使用してください –