Enhance your list items created with ngFor by incorporating a variety of icons using Ionic

Is there a way to assign custom icons to individual items in a list created with ngFor? Would it be more effective not to automatically generate the menu items?

app.html

<ion-menu [content]="mainContent">
<ion-content id="side-menu" style="background-color:#7A5258;">
    <ion-list no-lines id="menu-list">
        <button menuClose ion-item *ngFor="let p of pages" (click)="openPage(p)">
            <ion-icon name="ios-bookmark"></ion-icon>
            {{p.title}}
        </button>
    </ion-list>
</ion-content>

app.component.ts

export class MyApp {
@ViewChild(Nav) nav: Nav;
rootPage: any = HomePage;

pages: Array<{ title: string, component: any }>;

constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {
    platform.ready().then(() => {
        statusBar.styleDefault();
        splashScreen.hide();
    });

    // Customizing menu items for ngFor
    this.pages = [
        {title: 'Home', component: HomePage},
        {title: 'Bookmarks', component: BookmarksPage},
        {title: 'Help', component: HelpPage},
        {title: 'Settings', component: SettingsPage},
    ];
}

openPage(page) {
    this.nav.setRoot(page.component);
}

}

Answer №1

You must include it in the pages array as demonstrated below.

Please Note: Feel free to alter your icons to your liking.

app.component.ts

    // ngFor menu items
    this.pages = [
        {title: 'Home', component: HomePage, icon: 'play'},
        {title: 'Bookmarks', component: BookmarksPage, icon: 'play'},
        {title: 'Help', component: HelpPage, icon: 'play'},
        {title: 'Settings', component: SettingsPage, icon: 'play'},
    ];

app.html

<ion-menu [content]="mainContent">
<ion-content id="side-menu" style="background-color:#7A5258;">
    <ion-list no-lines id="menu-list">
        <button menuClose ion-item *ngFor="let p of pages" (click)="openPage(p)">
           <ion-icon [name]="p.icon" item-left></ion-icon> 
            {{p.title}}
        </button>
    </ion-list>
</ion-content>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Validation parameters provided during the Palantir workshop

At our Palantir workshop, we utilize a form to input values that are then added to an Ontology object. Recently, I've been tasked with validating the combination of userId, startdate, and states from the form inputs to check if they already exist or n ...

Navigating URLs to index.html for localized Angular application within lighttpd server - a guide

When deploying an Angular application to a lighttpd server, if a user is browsing example.com/product/12 and sends the link to someone else, they may encounter a 404 error without proper URL rewriting. In this scenario: localized versions are stored in s ...

Angular 8 does not retain class variables once the subscribe function is finished

After calling the fetchCounties() method within the subscribe function, the value assigned to the class variable counties does not persist. When I log the temporary variable data, it shows a valid list of counties, as well as this.counties inside the sub ...

"Encountering a Cypress Angular error: CypressError with the message 'Timed out retrying: Expected content was not

During my Cypress test run, I encountered an error message on the last step (click) which stated: Timed out retrying: Expected to find element: .button-darkblue, but never found it. Here is the code snippet: describe('Test Login', () => { i ...

Guide on saving a PDF file after receiving a binary response using axios

Is there a way to save a PDF file from a binary response received through an axios get request? When making the request, I include the following headers: const config: AxiosRequestConfig = { headers: { Accept: 'application/pdf', respon ...

Angular does not support custom validation as effectively as other frameworks

I am encountering an issue with my Angular form builder where I cannot determine the type of a file after reading it in my custom validation. Here is the link to the code on StackBlitz: https://stackblitz.com/edit/angular-ivy-atwqqc?file=src%2Fapp%2Fapp. ...

Guide to transferring token specifications to a different webpage using Angular 2

I have successfully implemented JWT login in my Angular2 application. After logging in, I want to display the first and last name of the user on the profile page. However, being new to Angular, I am seeking guidance on how to achieve this. Below is my cod ...

Ways to display or conceal a component based on the current component being used

I am a newcomer in Angular, and I believe finding the solution to my problem will be a great learning experience. In my default component, the MainComponent is loaded in my <router-outlet>. At this stage, both the menu and the footer are displayed. H ...

Encountering issues with Proxy functionality in the latest versions of Angular 13 and Spring Boot

I've encountered an issue with the proxy configuration in Angular. I'm unsure if it's a problem within my Angular settings or if there's a configuration issue in Spring. For testing purposes, I have a backend built in springboot to han ...

How can we declare React context functions to avoid any potential linting problems?

Working with React Context, I currently have: const DocumentContext = createContext< [DocumentStateType, React.Dispatch<any>, React.Dispatch<any>] >([initVal, () => { }, () => { }]); However, I am receiving a complaint from my ...

When trying to reload Angular 8 pages, encountering an error that reads "Cannot GET /login" and also receiving a notification stating the image '/favicon.ico' cannot be loaded due to a violation of

Encountering an issue with the error message "Cannot GET login/" appearing on the page body of my latest Angular 8 app. Despite attempting various solutions found on forums, I have been unable to resolve this error. Any suggestions or advice would be great ...

The BooleanField component in V4 no longer supports the use of Mui Icons

In my React-Admin v3 project, I had a functional component that looked like this: import ClearIcon from '@mui/icons-material/Clear' import DoneIcon from '@mui/icons-material/Done' import get from 'lodash/get' import { BooleanF ...

When employing the pipe function within *ngFor, the webpage's refresh may vary, causing occasional updates

Utilizing angular2-meteor, I have already implemented pure: false. However, the pipe seems to be running inconsistently. For more details on the issue, please refer to my comments within the code. Thank you. <div *ngFor="#user of (users|orderByStatus) ...

Utilizing React with .NET 8.0 and Minimal API, the front end is configured to send HTTP requests to its own specific port rather than the

Transitioning from working with react projects on .NET 3.1 to creating a new project on .NET 8.0 has been a challenging and confusing experience for me. When I attempted to access a newly created controller method, I encountered the error 404 Not Found. It ...

Creating various subtypes for graphql-codegen

Currently, I am utilizing the typescript-operations package within the framework of the graphql-codegen library. Previously, I was accustomed to using Apollo's deprecated codegen and appreciated how it exported types seamlessly. For example, let&apos ...

Choose a row that does not have the checkbox selected

Currently, I am using the ag-grid library which can be found at https://www.ag-grid.com After experimenting with various grid options such as rowSelection: 'multiple', suppressRowClickSelection: true, and rowDeselection: true, I encountered an i ...

Setting the initial selected option in a dropdown using Angular 4 Reactive Forms

One of the challenges I faced in Angular 4 was displaying a dropdown list with countries using a Reactive module. To achieve this, I had set up a configuration in a json file as follows: countries: ['USA', 'UK', 'Canada']; ...

Running into issues with TypeScript in conjunction with Redux-Form and React-Redux connect

My excitement for TypeScript grew until I encountered some frustrating incompatibilities between Redux-Form and React-Redux. I am aiming to wrap a component decorated with reduxForm using the connect decorator from react-redux—this method has always bee ...

My application is experiencing issues due to a malfunction when refreshing with query parameters in Angular 2

My URL contains query parameters, and after a successful transaction that clears those parameters, pressing the back button causes them to reappear. Additionally, if I refresh the page, an illegal action occurs - the same transaction is repeated and data ...

Error: No default Firebase App named '[DEFAULT]' exists. Please remember to call Firebase App.initializeApp() to create the app (app/no-app). This issue is located at the app

Currently, I am in the process of learning how to integrate Firebase Functions into an Ionic + Angular project. My goal is to develop a custom function that retrieves all games from a collection and returns an array sorted by the "count" attribute. Initia ...