How can I select an element in a component template?

后端 未结 12 2179
挽巷
挽巷 2020-11-21 06:56

Does anybody know how to get hold of an element defined in a component template? Polymer makes it really easy with the $ and $$.

I was just

12条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-21 07:12

    Selecting target element from the list. It is easy to select particular element from the list of same elements.

    component code:

    export class AppComponent {
      title = 'app';
    
      listEvents = [
        {'name':'item1', 'class': ''}, {'name':'item2', 'class': ''},
        {'name':'item3', 'class': ''}, {'name':'item4', 'class': ''}
      ];
    
      selectElement(item: string, value: number) {
        console.log("item="+item+" value="+value);
        if(this.listEvents[value].class == "") {
          this.listEvents[value].class='selected';
        } else {
          this.listEvents[value].class= '';
        }
      }
    }
    

    html code:

    • {{ event.name }}

    css code:

    .selected {
      color: red;
      background:blue;
    }
    

提交回复
热议问题