3

I have a text in which I make annotations that are dynamically marked with a span. With the function 'addClickEvent' I add a click-event, so that the user gets more information when clicking on the annotations. In the console I can get all information after the click. But I can't bind the information with the HTML. Thank you very much for your hints.

HTML

  <div id="description">
   <span class="dictCanon">{{descriptionDictCanon}}</span>
   <span class="coveredText">Covered text: {{descriptionCoveredText}}</span>
   <span class="sectionHeader">Category</span>
   <span class="val">{{descriptionCategory}}</span>
   <span class="sectionHeader">Group</span>
   <span class="val">{{descriptionGroup}}</span>
   <span class="sectionHeader otherSynonyms">Synonyms</span>
   <span class="val">{{descriptionSynonyms}}</span>
  </div>

JS

addClickEvent (obj) {

var rangyRoot = document.getElementById("rangy");
var elems = rangyRoot.getElementsByClassName('marker');

if (elems.length) {
  for (var i = 0, l = elems.length; i < l; i++) {
    (function(i) {
      elems[i].onclick = function() {
        var termToFind = elems[i].innerHTML;
        for (var item in obj) {
          var str = obj[item].coveredText;
          if (str === termToFind) {

             let d = obj[item];

             this.descriptionDictCanon = d.dictCanon;
             this.descriptionCoveredText = d.coveredText;
             this.descriptionCategory = d.parentCategory;
             this.descriptionGroup = d.parentGroup;
             this.descriptionSynonyms = d.otherSynonyms;

            };
          };
        }
      })(i);
    }
  }
}
2
  • 2
    You're doing it all wrong, using Angular as if it was jQuery. Don't get elements by ID. Don't do DOM manipulation. Don't add event listeners from your code. Expose a model from your component, and use directives and binding in the template (*ngFor, *ngIf, (click), etc.) to display this model. When you want to change what is displayed, change the model, and angular will update the view.
    – JB Nizet
    Commented Dec 13, 2018 at 7:35
  • If you want to do DOM manipulation in angular. Angular has special API to do that check this:angular.io/api/core/Renderer2 Commented Dec 13, 2018 at 7:41

1 Answer 1

0
<div id="description">
   <span class="dictCanon" (click)="spanCLicked(descriptionDictCanon)">{{descriptionDictCanon}}</span>
.
.
.
  </div>

and in your ts

spanCLicked(item: any)
{
    console.log(item)
}
1
  • For the annotations, I use rangy. Unfortunately It is not possible to add an click event to the span
    – mm1975
    Commented Dec 13, 2018 at 8:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.