Angular 4服务文件参考显示为未定义

时间:2017-11-05 22:33:22

标签: angular

我的应用正在尝试访问服务文件以供参考以执行方法。我一直在控制台中收到以下错误:

Uncaught TypeError: Cannot read property 'keytermsUpdateService' of undefined
    at HTMLDivElement.froalaEditor.keyup (editor.component.ts:51)
    at HTMLDivElement.dispatch (scripts.bundle.js:4)
    at HTMLDivElement.q.handle (scripts.bundle.js:4)
    at Object.trigger (scripts.bundle.js:5)
    at r.fn.init.triggerHandler (scripts.bundle.js:5)
    at v (scripts.bundle.js:13)
    at HTMLDivElement.<anonymous> (scripts.bundle.js:13)
    at HTMLDivElement.dispatch (scripts.bundle.js:4)
    at HTMLDivElement.q.handle (scripts.bundle.js:4)
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:425)

我检查了函数的范围并检查了构造函数,但是我找不到错误。

这是我的组件文件:

import { Component, ViewEncapsulation } from '@angular/core';
import { Editor}  from './editor';
import { KeytermsService }  from '../keyterms/keyterms.service';
import rake from 'rake-js';
import striptags from 'striptags';
declare var $: any;

@Component({
  selector: 'app-editor',
  templateUrl: './editor.component.html',
  styleUrls: ['./editor.component.css'],
  encapsulation: ViewEncapsulation.None
})

export class EditorComponent {
  title = 'editor';
  statusCode: number;

  editor: Editor[];
  allEditor: Editor[];

  constructor(public keytermsService: KeytermsService) { 

   }

  ngOnInit(): void {

      $.FroalaEditor.DefineIcon('alert', {NAME: 'info'});

      let keywords;
      this.keytermsService.keytermsUpdateService(keywords);
  }

  public options: Object = {
    placeholderText: 'Edit Your Content Here!',
    charCounterCount: false,
    height: "70vh",
    placeholder: "Edit Me",
    events : {
      'froalaEditor.keyup' : function(e, editor) {
        let html = (editor.html.get());
        let text = striptags(html);
        console.log(text);
        let ec = new EditorComponent(this.keytermsService);
        let keywords = rake(text, { language: 'english' });
        this.keytermsService.keytermsUpdateService(keywords);
      }
    }
  }

无论我附加到keytermsUpdateService的函数是什么,我都会收到错误。

1 个答案:

答案 0 :(得分:0)

可能是因为您在构造函数中使用 public ,使用 private 作为注射剂。还记得在服务提供商处注册您的服务 NgModule。