0

im working on a angular 6. im changing the colors of a div tag from a script after a click function i need to change it back to transparent

Here is my View when i click the inheritance first line gets background color gets changed

after clicking inheritance when i click the Method Overriding the second lines get highlighted But i need to remove the background color from first line

enter image description here

Html code for side bar

  <div *ngFor="let classes of classesObject ">
    <input id='{{classes}}' name='radio' type='radio'>
    <label (click)="clickClass(classes)" for='{{classes}}'>
      {{classes}}
      <div class='lil_arrow'></div>
      <div class='bar'></div>
      <div class='swanky_wrapper__content'>
        <ul *ngFor="let conceptName of concepts">
          <li (click)="getConcepts(conceptName['conceptName'],[classes])">{{conceptName["conceptName"]}}</li>
        </ul>
      </div>
    </label>
  </div>

Html code for line display

 <div *ngFor="let lines of lineList; let i= index">
    <div id="line_{{i}}" class="code" >&nbsp;&nbsp;{{lines}}</div>
 </div>

Typescript code for getConcept method

  for (var no in this.relatedLineNo) {
    var lines = this.relatedLineNo[no] - 1;
    document.getElementById(`line_` + lines).style.backgroundColor = "black";
    document.getElementById(`line_` + lines).style.color = "white";
    document.getElementById(`line_` + lines).style.cursor = "pointer";
    document.getElementById(`line_` + lines).setAttribute("tooltip", this.messageTip);
  }

1 Answer 1

2

Instead of manually looking up lines in a for loop with this:

document.getElementById(

Try a bit more Angular-ish approach:

[ngClass]="{ hilite: i === relatedLineNo }"

What happens is that inside your *ngFor loop Angular will evaluate current line index "i" with relatedLineNo and will "automatically" highlight the line.

UPDATE

If "relatedLineNo" is an array then you can use this:

[ngClass]="{ hilite: relatedLineNo.indexOf(i) !== -1 }"

Working and updated stackblitz.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.