1

In a directive.ts file, I would like to modify a DOM element. When I do a browser debug, and stop in the directive, I can inspect its elements, but I do not know how to access the elements in typescript as I see them in the debugger.

Here's the relevant ts code:

  drawSparklines() {
    console.log("drawSparklines");

    $('.sparkline:not(:has(>canvas))', this.container).each((i, el) => {
      const $el = $(el);
      const sparklineType = $el.data('sparkline-type') || 'bar';

      if($el.nativeElement.innerText === "dataIncome") {
        $el.nativeElement.innerText.val = this.dataIncome;
      }

I added the conditional, above, where I am trying to inspect the innerText value, and change it if it equals dataIncome to a class property.

In the attached screen shot taken in the debugger, you can see the innerText property. But, how to actually work with it in ts?

Debugger Screen Shot:

enter image description here

Thanks for helping! Bob

BobC
  • 353
  • 2
  • 5
  • 17

3 Answers3

1

Take a peek at the StackBlitz example included in below thread, it might help push you in the right direction:

How can I select an element in a component template?

cheers

iHazCode
  • 622
  • 4
  • 15
  • Thanks iHazeCode. That is a useful read. I followed it a few months ago when I was trying to solve problem, that the topic spells out nicely for using @ViewChild. But there wasn't much there for my current problem. I did solve my using trial&error on the DOM element. I will describe it in my answer. – BobC Jul 10 '19 at 07:57
1

I solved the problem using trial&error in the debugger. I was actually trying to make it more complicated than needed. No need for jQuery $el, and nativeElement.

Here's what worked:

  drawSparklines() {
    console.log("drawSparklines");

    $('.sparkline:not(:has(>canvas))', this.container).each((i, el) => {
      const $el = $(el);
      const sparklineType = $el.data('sparkline-type') || 'bar';

      if(el.innerText === "dataIncome") {
        el.innerText = this.dataIncome;
      }

Thanks, Bob

BobC
  • 353
  • 2
  • 5
  • 17
0

it's two way first: in component file:

document.getElementById("");

and the second way if you modify element to use this:

@Component({
  selector: "",
  templateUrl: "",
  host: {
    "(document:click)": "onClickOutsideDropdown($event)"
  }
})
 constructor(private el: ElementRef) {
  }
 onClickOutsideDropdown(event: Event) {
     this.el.nativeElement.contains(event.target) ...
  }
mosi98
  • 575
  • 6
  • 16