Angular and TypeScript allow for dynamic binding after a click event

My text has annotations marked dynamically with spans. By using the 'addClickEvent' function, I'm able to add a click-event to provide users with more information when they click on the annotations. Although I can retrieve all the information in the console after a click, I'm facing difficulty in binding this information with the HTML. Any suggestions or tips would be greatly appreciated.

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);
    }
  }
}

Answer №1

<div id="details">
   <span class="dictionaryTerm" (click)="handleClick(detailsDictionaryTerm)">{{detailsDictionaryTerm}}</span>
.
.
.
  </div>

and within your typescript file

handleClick(term: any)
{
    console.log(term)
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adding two headers to a post request in Angular 2 - a step-by-step guide!

Is there a way to combine 2 headers in the following code snippet: appendHeaders(json: PortfolioVO) { var newJson = JSON.stringify(json); var allHeaders = new Headers(); allHeaders.append('Content-type' , 'application/jso ...

How to disable the first option in an Angular 2 select dropdown

I'm working with a select component, and here is the code snippet I have: <select name="typeSelection" materialize="material_select" [(ngModel)]="trainingplan.type" > <option [ngValue] = "null" disabled selected>Please choose a ...

Leveraging Amazon IVS Player within Angular

Struggling to integrate the npm version of the amazon-ivs-player with Angular, as it seems optimized for webpack while I am using angular-cli. Following a guide at this link. The issue arises with importing the wasm files in my Angular application: ERROR ...

I am unable to add a new property to the request object in the Express framework

My goal is to add a new property to the request object in typescript. Here's the code snippet I'm using: import { request, Request, response, Response } from "express"; ((req: Request, res: Response) => { console.log(req.user); ...

Managing Deactivated Elements in Ionic Web Edition

In my extensive ionic application that I deploy to both the Web and mobile platforms, a member of the testing team brought to my attention an issue regarding disabled elements. I have numerous elements such as buttons, checkboxes, inputs, etc., which can b ...

Transforming JSON in Node.js based on JSON key

I am having trouble transforming the JSON result below into a filtered format. const result = [ { id: 'e7a51e2a-384c-41ea-960c-bcd00c797629', type: 'Interstitial (320x480)', country: 'ABC', enabled: true, ...

Issues with Angular's *ngIf directive not functioning correctly

Within my template, I have a block of HTML that controls the visibility of a div. <div *ngIf="csvVisible"> <p>Paragraph works</p> </div> This particular code snippet belongs to my component. export class SettingsComponent imple ...

Discover the geolocation data for post code 0821 exclusively in Australia using Google Maps Geocoding

I'm having trouble geocoding the Australian postcode 0821. It doesn't seem to reliably identify this postcode as being located within the Northern Territory, unlike 0820 and 0822 which work fine. Here's an example of what I'm doing: ...

Guide on implementing a cordova plugin in a TypeScript cordova application

In my Cordova project, which utilizes Angular and Typescript, I am facing issues with implementing the juspay-ec-sdk-plugin for Cordova. I have explored possible solutions on StackOverflow related to integrating Cordova plugin in an Angular 4 Typescript ...

I continuously encounter an issue in Vite version 3.2.4 where an error pops up stating `[vite:esbuild] The service has stopped running: write EPIPE`

When I finished creating a Vite app, I ran the command npm run dev and encountered the following error: [vite:esbuild] The service is no longer running: write EPIPE https://i.stack.imgur.com/MZuyK.png I need help solving this error. Can anyone provide gu ...

The error TS2304 in @angular/common is indicating that the name 'unknown' cannot be found

I am struggling to revive an old project due to some errors. I am using Angular 5.2.9 for the build, but these errors keep popping up. If anyone can offer assistance, I would greatly appreciate it. This is how my package.json file appears: "dependencies" ...

Deploying Angular 6 on Heroku with modifications

For my angular 5 app, I utilized this express code for deployment. const express = require('express'); const path = require('path'); const app = express(); app.use(express.static(__dirname + '/dist')); app.get('/*&a ...

Passing a variable from index.html to a script in Angular

I am attempting to pass the array variable 'series' to the script in HTML. Is there a way to do this? app.component.ts import { Component } from '@angular/core'; @Component({ selector: 'my-app', templateUrl: './app ...

Angular project models

I'm exploring the Core/Shared/Feature design pattern for building large, scalable applications in Angular, and I find myself unsure about where models fit in. Is there a consensus on which module they belong in? I came across a post suggesting that ...

Every time I clear the information, it seems to be instantly replaced with new data. How can I prevent it from constantly refilling?

When I press the remove button on my application, it successfully deletes the data in a field. However, it also automatically adds new data to the field which was not intended. I am seeking assistance on how to keep those fields empty after removing the ...

When object signatures match exactly, TypeScript issues a warning

I am facing an issue with typescript while trying to use my own custom type from express' types. When I attempt to pass 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>' as a parameter of type 'Context&a ...

What is the best way to create a highlighted navigation bar using Angular 2?

Is there a way to keep the navbar active even after refreshing the page in Angular 2? I am currently using CSS to accomplish this, but the active tab is removed upon page refresh. Any suggestions on how to tackle this? HTML:-- <ul class="nav navbar- ...

Exploring the use of two different array types in the useState hook with TypeScript

Working on a movie gallery project, I am utilizing an API to download movies and TV series. They are then displayed in a Row component where users can click on thumbnails to open them. The challenge arises with TypeScript, as the useState array can receiv ...

Having difficulty incorporating TypeScript into Vue

A little while ago, I set up a vue project using vue init webpack . and everything was running smoothly. Recently, I decided to incorporate typescript and ts-loader. I created a file in the src directory with the following content: declare module '* ...

Choosing options using an enum in Angular 2

In my TypeScript code, I have defined an enum called CountryCodeEnum which contains the values for France and Belgium. export enum CountryCodeEnum { France = 1, Belgium = 2 } Now, I need to create a dropdown menu in my form using this enum. Each ...