Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

change icon on click angular

import { Component } from '@angular/core'

export interface CollapsibleItem { 
    label: string; 
    text: string;
    state: boolean;
}

@Component({
selector:'whateveryouwant',
template: `

<ul class="collapsible" data-collapsible="accordion">
    <li *ngFor="let item of menuItems; let i = index" (click)="menuClick(i)">
       <div class="collapsible-header">
         <i class="material-icons" *ngIf="item.state"> minus-icon </i>
         <i class="material-icons" *ngIf="!item.state"> plus-icon </i>
         {{ item.label }} 
       </div>
       <div class="collapsible-body">
         <span> {{ item.text }} </span>
       </div>
    </li>
`,
styles: ['']  
})

export class YourComponentName {

   constructor() {  }  

    menuItems: CollapsibleItem[] = [
    { label: 'First', text: 'Lorem Ipsum', state: false },
    { label: 'Second', text: 'Lorem Ipsum', state: false },
    { label: 'Third', text: 'Lorem Ipsum', state: false },
   ];

    menuClick(clickedItem: number) {
        this.menuItems[clickedItem].state = !this.menuItems[clickedItem].state  // flips the boolean value for the clicked item 
        for (let item of this.menuItems) {  
           if ( item !== this.menuItems[clickedItem] ) { 
               item.state = false; 
           }
        }
        // the for loop goes through the array and sets each item to false *if* its not the item that was clicked
     }   
 }
Comment

PREVIOUS NEXT
Code Example
Javascript :: error: Unknown dialect undefined 
Javascript :: javascript compare dates 
Javascript :: javascript constants 
Javascript :: promises in javascript 
Javascript :: mongoose populate array of ids 
Javascript :: adding parameters to url react router 
Javascript :: addeventlistener js 
Javascript :: apply css to shadow dom 
Javascript :: javascript table functions 
Javascript :: run two function after one another 
Javascript :: make query param optional node 
Javascript :: web3.js tutorials 
Javascript :: react text to link 
Javascript :: async.each javascript 
Javascript :: toast show pb 
Javascript :: javascript reflect 
Javascript :: jwt token npm 
Javascript :: sequelize attributes exclude all 
Javascript :: how to select a dom element in react 
Javascript :: use navigation in class component react native drawer navigation 
Javascript :: concatenate arrays javascript 
Javascript :: do while loop javascript 
Javascript :: how to compile typescript to javascript es6 
Javascript :: Agora Video Calls 
Javascript :: syntax of ternary operator in javascript 
Javascript :: check property exists 
Javascript :: javascript Insert Item to Map 
Javascript :: syntax of the ternary operator 
Javascript :: use index of an array within a for loop 
Javascript :: for in and for of in js 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =