Mastering the art of Interpolation and Binding in Ionic 3/Angular 4: A step-by-step

My goal is to retrieve data from my Parse Server where MongoDB is installed.

Although I have successfully displayed the data in the console, I am facing issues interpolating them in the HTML template.

Here is my search.ts file:

import { localData } from './../../services/local';
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Parse } from 'parse';
import 'rxjs/add/operator/map';
import {User} from '../../models/User';

@Component({
selector: 'page-search',
templateUrl: 'search.html'
})

export class SearchPage {
currentUser = Parse.User.current();
query1 = new Parse.Query('Friends');
friendQuery = new Parse.Query('Friends');
query = new Parse.Query(Parse.User);
tmp: any;
users: any= [];
user: any= [];

loadUsers (){
this.tmp = this.localdata.getUsers();
}

initializeItems() {
 this.users;
}

constructor(public navCtrl: NavController, private localdata: localData) {
  this.query.find().then(data => {
  this.tmp = data; 
  console.log(this.tmp);
  this.localdata.setUsers(this.tmp);
  this.users = this.localdata.getUsers();
  console.log (this.users);
  console.log(this.users.username) 
   })
  }


getItems(ev: any) {
// Reset items back to all of the items
this.initializeItems();

// set val to the value of the searchbar
let val = ev.target.value;

// if the value is an empty string don't filter the items
if (val && val.trim() != '') {
 this.users = this.users.filter((user) => {
 return (user.username.toLowerCase().indexOf(val.toLowerCase()) > -1);
 })
 }
 }
 }

This is the local.ts file where I save the array of Users

import { Component } from '@angular/core';

export class localData {

setUsers (users){
    window.localStorage.users_data = JSON.stringify(users);
}
getUsers(){
   return JSON.parse(window.localStorage.users_data || '[]');
}

}    

And finally, here is my HTML template search.html:

<ion-header>
<ion-navbar>
<ion-searchbar
(ionInput)="getItems($event)" 
(ionCancel)="onCancel($event)"
[animated]= true>
</ion-searchbar>
</ion-navbar>
</ion-header>

<ion-content padding>
<ion-item class="item-avatar item-avatar-left item-button-right common-list" *ngFor="let user of users"></ion-item>
<div></div>


</ion-content>

In the HTML template, inside the div, I am trying to use user.username but it is not working as expected.

Can someone please advise me on how to display it correctly?

Thank you so much!

Answer №1

To create your template in Ionic, make sure to follow the structure below.

 <ion-content padding> 
    <ion-list>
        <ion-item *ngFor="let user of users">
          <ion-label>{{user.username}}</ion-label>
        </ion-item>
    </ion-list>
 </ion-content>

Answer №2

Based on the JSON screenshot you provided, there seems to be a missing 'username' property. To address this issue, consider implementing the following code snippet:

<ion-content padding>
<ion-item class="item-avatar item-avatar-left item-button-right common-list" *ngFor="let user of users">
  <h1> {{user.className}}</h1>
</ion-item>
<div></div>

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

Steps for converting an observable http request into a promise request

Here is a link to my service file: https://i.sstatic.net/8dsMx.png This is the TypeScript file for my components: https://i.sstatic.net/of6sx.png And these are the response models: https://i.sstatic.net/U8RUQ.png https://i.sstatic.net/7baTj.png I am curre ...

Generating Typescript definition files from JavaScript files with module.exports assignment

I'm currently working on creating a custom TypeScript definition file for format-duration: module.exports = (ms) => { let { days, hours, minutes, seconds } = parseMs(ms) seconds = addZero(seconds) if (days) return `${days}:${addZero(hours)}: ...

An object resulting from the combination of two separate objects

After reading a helpful solution on StackOverflow about merging properties of JavaScript objects dynamically, I learned how to utilize the spread operator in Typescript. However, one question still remains unanswered - what will be the type of the object c ...

Encountered a RunTime Error when attempting to Lazy Load a module

When attempting to lazy-load a component separately, an unexpected run-time error is occurring. This issue is specifically related to writing loadChildren within the routing module of another component in Angular 6. Any assistance with resolving this error ...

Merging Two Individual Applications into a Single Application Using Angular 8

Recently started learning Angular as a beginner. I am currently working on developing an application and opted to utilize the Angular Multikart ecommerce template. This template comes with separate front-end and admin-end sections, which I can currently ...

Is there a way for me to dissect a segment of the source map produced by source-map-explorer without any reference?

I'm currently working on an Angular 12 application and I've noticed that the bundle size is unexpectedly large, even though there are only a few components in the application. After using source-map-explorer to analyze the bundle, I discovered th ...

Difficulty encountered while trying to link an abstract class with Redux state using redux-thunk

My approach to using redux-thunk and class components in React follows a basic pattern. The code below shows how a class definition MyClass is exported, correctly connected to state through mapStateToProps, and has access to actions from mapDispatchToProps ...

TSConfig - Automatically Generates a ".js" File for Every ".ts" File

Having a unique software application with an unconventional file structure project ├── tsconfig.json ├── app_1 │ ├── ts │ └── js | └── app_2 ├── ts └── js I aim to compile files located inside the ...

Exploring the world of Unicode in Angular

I need to search for pokemon and retrieve all Pokémon results (with the accent included) This is my array: let games = ['The Legend of Zelda', 'Pokémon', 'Chrono Trigger'] This is how I am attempting to do it: Using HTML ...

Access to Oracle Cloud exclusive to Angular Framework, omitting AngularJS and Mid-Tier

Exploring Oracle Cloud for the first time and embarking on a self-learning journey, I have developed an Angular application (not AngularJS) where I aim to upload images into my Oracle Cloud Object Storage Bucket. My goal is to accomplish this using only An ...

Can someone please share the steps on how to insert a new row into a PrimeNg DataTable component

I am currently working on enhancing a table by adding a new row underneath the existing data list. This new row will contain a save button and allow users to insert information, such as saving a phone number. While I have indicated where the new row should ...

For each array element that is pushed, create and return an empty object

I am encountering an issue with an array where the objects are being generated by a push operation within a function. Despite successfully viewing the objects directly in the array, when I attempt to use forEach to count how many times each id uses the ser ...

Clicking on hyperlink within email to open in default web browser

I am facing a problem with my Angular web app. Here is the scenario of the issue: When I send a link to my app via email and try to open it from a mobile email client using a short version of a browser or webview (not sure what it's called), I encou ...

TypeScript - Variable is inferred to have type 'any' in certain locations where its type cannot be accurately determined

I'm attempting to generate an MD5 hash from the content of an uploaded file. I am trying to set a global declaration to be used within functions, but I encounter an error when trying to utilize it: The error message states - Variable 'hasher&apos ...

Tips for addressing a situation where an npm package encounters issues during AOT compilation

As a newcomer to web development, I am currently struggling with an issue that has me stumped. Within my web application, I am utilizing the npm package called @uniprank/ngx-file-uploader (https://www.npmjs.com/package/@uniprank/ngx-file-uploader). While ...

What is the process for establishing the default type for an Activity Entity in Microsoft Dynamics?

Currently in the process of restructuring a portion of script code associated with the Fax Activity Entity within Microsoft Dynamics. Within the script code, the following can be found: document.getElementById("regardingobjectid").setAttribute("defaulttyp ...

What is the process for integrating Typescript into a Quasar 2 project that is utilizing Vite as its build tool?

The Quasar 2 documentation provides in-depth guidance on integrating Typescript with Webpack: Unfortunately, my Quasar project is configured with Vite and I am struggling to locate resources on incorporating Typescript into an already existing project. A ...

Sequelize Date Range Error When Using Op.between with TypeScript

My goal is to retrieve all records from a MySql table that were created within a specific date range. To accomplish this, I created the following code snippet: import { Sequelize, Model, DataTypes, Op } from 'sequelize'; const sequelize = new ...

Save the application's state of the user in a mean stack program

In my Angular 9 application, I have forms with multiple sections. The first section includes name and personal details, the second part covers primary school information, and the third part focuses on past jobs held by the user. Each part is displayed in a ...

Issue with displaying children in Angular 2 router: children components not appearing on the

I am currently in the process of incorporating a user authentication bundle into my Angular 2 project by following this tutorial at . I have organized all the files from the bundle into a parent folder named "loginMGR", and modified the module, components ...