2

I have create a method that that require a anonymous method as parameter. as follows

getannotationContent(): any {
    var searchString = 'cash';
    var strLength = searchString.length;
    var doc = this.getReaderControl().docViewer.getDocument();
    let data = doc.searchText('cash', 'CaseSensitive,WholeWord');
    doc.loadPageText(3, function (text) {
      var start = 0;
      var index;
      while ((index = text.indexOf(searchString, start)) !== -1) {
        doc.getTextPosition(3, index, index + strLength,
          this.highlightText);
        start = index + strLength;
      }
    });
  }

Now i also declare and initialize this anonymous method as follows

highlightText(quads): any {
    debugger;
    const viewerWindow = this.webviewerComponent.getWindow();
    var docViewer = this.getReaderControl().docViewer;
    var am = docViewer.getAnnotationManager();
    let anf = am.getAnnotationsList();
    var firstChar = quads[0];
    var lastChar = quads[quads.length - 1];
    var firstx = (firstChar.x1 + firstChar.x2) / 2;
    var finalx = (lastChar.x3 + lastChar.x4) / 2;
    var y = (firstChar.y1 + firstChar.y4) / 2;
    var topLeft = { x: firstx, y: y, pageIndex: this.pageNum };
    var bottomRight = { x: finalx, y: y, pageIndex: this.pageNum };

    var annot = new viewerWindow.Annotations.TextHighlightAnnotation();
    annot.setPageNumber(this.pageNum);
    annot.StrokeColor = new viewerWindow.Annotations.Color(0, 255, 255);
    am.addAnnotation(annot);

    var textHighlightTool = new viewerWindow.Tools.TextHighlightCreateTool(docViewer);
    textHighlightTool.annotation = annot;
    textHighlightTool.pageCoordinates[0] = topLeft;
    textHighlightTool.select(topLeft, bottomRight);

  }

Now when we run application than this.highlightText() method is shows as undefined. so can you please tell me how to pass anonymous function inside a function as parameter in angular4.

Thanks

Manoj Gupta

Manoj Gupta
  • 456
  • 4
  • 9

1 Answers1

2

You need to use arrow function like this doc.loadPageText(3, (text) => {}).

Your this in this.highlightText refers to the wrong scope.

Hence replace your function definition with the below code:-

getannotationContent(): any {
    var searchString = 'cash';
    var strLength = searchString.length;
    var doc = this.getReaderControl().docViewer.getDocument();
    let data = doc.searchText('cash', 'CaseSensitive,WholeWord');
    doc.loadPageText(3, (text) => {
      var start = 0;
      var index;
      while ((index = text.indexOf(searchString, start)) !== -1) {
        doc.getTextPosition(3, index, index + strLength,
          this.highlightText);
        start = index + strLength;
      }
    });
  }
Ankit Sharma
  • 1,674
  • 8
  • 11