简体   繁体   中英

Angular 6 + get elements with class name from document and change style

I have a set of "tabs" in my HTML page. They are nothing but "div" with a class name "tab". My intention is to show one tab at a time and use "Next" and "Previous" buttons to access other tabs. HTML code looks like this.

<div class="container">
  <div *ngFor="let question of questionList;let i = index" class="questionBox tab">


    {{question.question.query}}

    <br>
    <div *ngFor="let option of question.options">
      <input type="radio" value="{{option.id}}" name="radio_{{i}}" [(ngModel)]="question.selected.id">{{option.text}}
      <br>
    </div>
    {{question.selected | json}}

    <br>
    <br>
    <!--TO DO -->
    <input type="button" class="btn btn-primary" value="Previous">
    <!--TO DO -->
    <input type="button" class="btn btn-primary nxtButton" value="Next">
  </div>
  <input type="button" class="btn btn-success btn-block" value="Submit">
</div>

Initially all the tabs are hidden with CSS.

.tab{
    display: none; 
}

on page initialization, first tab has to be displayed and rest are hidden. showTab() method in the component class is intended for that purpose. shotTab method accepts a "number" that represents the index of the tab to be displayed.

  public showTab(n: number){

    let tabElements = document.getElementsByClassName("tab");
    let tabToDisplay = tabElements.item(n) as HTMLElement;
    tabToDisplay.style.display="block";
  }

to show the first tab, showTab() methos is invoked from ngOnInit() method of the component. but the line let tabElements = document.getElementsByClassName("tab"); does not return any result. ie the length of tabElements is 0. So the application will fail saying that " Cannot read property 'style' of null ".

AppComponent_Host.ngfactory.js? [sm]:1 ERROR TypeError: Cannot read property 'style' of null
    at AppComponent.push../src/app/app.component.ts.AppComponent.showTab (app.component.ts:25)
    at AppComponent.push../src/app/app.component.ts.AppComponent.ngOnInit (app.component.ts:18)
    at checkAndUpdateDirectiveInline (core.js:9243)
    at checkAndUpdateNodeInline (core.js:10507)
    at checkAndUpdateNode (core.js:10469)
    at debugCheckAndUpdateNode (core.js:11102)
    at debugCheckDirectivesFn (core.js:11062)
    at Object.eval [as updateDirectives] (AppComponent_Host.ngfactory.js? [sm]:1)
    at Object.debugUpdateDirectives [as updateDirectives] (core.js:11054)
    at checkAndUpdateView (core.js:10451)

Calling showTab() in ngOnInit() means it's called before the children have been initialized.

You need to use ngAfterViewInit() hook.

Full list of component lifecycle hooks .

According to your solution I solved my issue like this:

const myHtmlEl = document.getElementsByClassName('my-style').item(0) as HTMLElement;
myHtmltEl.style.display = 'none';

but I am sure I have only one item in whole page with class my-style. otherwise you should loop them with item.length. and apply style to each element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM