Unable to trigger click or keyup event

After successfully implementing *ngFor to display my data, I encountered an issue where nothing happens when I try to trigger an event upon a change.

Below is the snippet of my HTML code:

    <ion-content padding class="home">
      {{ searchString }}
      {{ selectedOption }}
      <ion-searchbar (keyup)="onKey(box.value)"></ion-searchbar>


      <ion-list radio-group>
        <ion-list-header>
          <span class="listHeader">Items</span><span class="pullRight">Showing 3 of 3</span>
        </ion-list-header>

        <ion-item *ngFor="let x of Options" (click)="changeSelection(x)">
          <ion-label>{{ x.displayName }}</ion-label>
          <ion-radio value="{{ x.id }}"></ion-radio>
        </ion-item>
      </ion-list>

</ion-content>

I was expecting the (click) event to execute and call the changeSelection method in the JavaScript file:

import {Component} from '@angular/core';
import {NavController} from 'ionic-angular';


@Component({
  templateUrl: 'build/pages/home/home.html'
})
export class HomePage {
    reportOptions;
    selectedOption;
    searchString;
    constructor(private navController: NavController) {

    this.Options = [    {
                                displayName: 'Test Option 1',
                                id: '1'
                            },
                            {
                                displayName: 'Test Option 2',
                                id: '2'
                            },
                            {
                                displayName: 'Test Option 3',
                                id: '3'
                            }
                          ];
    this.selectedOption = 'Hello';

    }

    changeSelection(x) {
        this.selectedOption = 'world';
    }
    onKey(value: string) {
        this.searchString= value;
    }

}

I can't seem to figure out what's missing here. Is there any specific import that I overlooked or something more complex at play?

Answer №1

It seems like you're facing an issue with correctly binding the elements to their respective properties using [(ngModel)].

For the ion-radio elements, make sure you have included the radio-group attribute.

<ion-list radio-group>

Don't forget to bind these radio buttons to the selectedOption property in your Component. By adding ngModel, the value of the selected radio button will be automatically assigned to the selectedOption. If you also want to capture the text from the radio button, use the ionSelect event:

<ion-radio value="{{ x.id }}" (ionSelect)="changeSelection(x)"></ion-radio>

Add the corresponding method in your component:

public changeSelection(x: any) {
    this.selectedOptionText = x.displayName;
}

Regarding the ion-searchbar, similarly, ensure that it is bound to a property in your component. Bind it to the searchString property using:

<ion-searchbar [(ngModel)]="searchString"></ion-searchbar>

If you need to execute some code when the blur event occurs, subscribe to one of the Output events from the ion-searchbar. For instance, to link the ionBlur event to the onBlur method, do as follows:

<ion-searchbar [(ngModel)]="searchString" (ionBlur)="onBlur()"></ion-searchbar>

You can check out a working example on Plunker showcasing your code in action.

Answer №2

If the changeSelection function was present in the code snippet you shared, it would be called upon. Currently, I do not observe its presence within your posted code.

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

Sending information to a service from the main.ts file in an Angular application

Within my Angular CLI application, I have a main.ts file that includes the following export statement. This file serves as a microservice that receives CONTEXT from another microservice. export default { async mount({ rootElement, context }: Extension) ...

Tips for accessing the FormControlName of the field that has been modified in Angular reactive forms

My reactive form consists of more than 10 form controls and I currently have a subscription set up on the valueChanges observable to detect any changes. While this solution works well, the output always includes the entire form value object, which includ ...

"Encountering an 'Access-Control-Allow-Origin' error in the VSCode Debug Console, even though the network tab in Chrome DevTools displays a 200OK

In my Angular 7 project, I encountered an issue while using HttpClient. When I click a button, the following code snippet is executed: this.http .get('http://localhost:30123/api/identity/name/' + this.name) .subscribe((answer: Identit ...

Guide on associating an array of object keys with an array of their corresponding values within a specified object

const obj = { wheels: 4, lights: 2, doors: 4 } customMapFunction(obj, { properties: ["wheels", "lights"], formatter: (wheels, lights) => `${wheels}-${lights}` // "4-2" }) How do I define the types for customMapFunction in TypeScript to ensure th ...

Issue with Angular's ngOnChanges Lifecycle Hook Preventing Function ExecutionWhen attempting to run a function within Angular's ngOn

In the midst of my evaluation process to ensure that specific values are properly transmitted from one component to another using Angular's custom Output() and EventEmitter(), I am encountering some issues. These values are being sent from the view of ...

Error: No routes found in Angular 5

I have developed an Angular 5 app and set up the following routing configuration in app-routing.module.ts file: import { ControlPanelComponent } from './dashboard/control-panel/control-panel.component'; import { MainComponent } from './dash ...

What are the steps to create a sliding carousel using ng2-bootstrap?

I've integrated the Angular2 Bootstrap carousel component from the following link: here, but I'm facing an issue with the transition and animation effects when sliding between items. Any suggestions on how to resolve this? ...

Having trouble retrieving the JSON data from the getNutrition() service method using a post request to the Nutritionix API. Just started exploring APIs and using Angular

When attempting to contact the service, this.food is recognized as a string import { Component, OnInit } from '@angular/core'; import { ClientService } from '../../services/client.service'; import { Client } from '../../models/Cli ...

Error encountered during Angular upload using Asp.net core: System.Text.DecoderFallbackException is thrown when bytes [FF] cannot be translated at the specified index

I am having trouble uploading a file using Angular 7 and Asp.net core 2.2 I have set up the Angular web service to append the file and include a property Name. However, when I call the WebService from Angular, I encounter an error in the Asp.net core l ...

The Perplexing Problem with Angular 15's Routing Module

After upgrading to Angular 15, I encountered an issue with the redirect functionality. The error message points out a double slash "//" in my code, but upon inspection, I couldn't find any such occurrence. * * PagesRoutingModule: const routes: Routes ...

Error TS2394: Function implementation does not match overload signature in Typescript

Take a look at this code that seems to be causing headaches for the TypeScript compiler: use(path: PathParams, ...handlers: RequestHandler[]): this use(path: PathParams, ...handlers: RequestHandlerParams[]): this use(...handlers: RequestHandler[]): this u ...

Enhancing the Angular Community Library Project

I am currently working on an Angular project version 7.1 and have developed 2 angular libraries that are being used in the project. In order to upgrade my project from version 7.1 to 8.2, I used the following command: ng update @angular/cli@8 @angular/core ...

Encountering a ReferenceError while attempting to implement logic on a newly created page

I've been experimenting with building a website using the Fresh framework. My goal was to add a simple drop-down feature for a button within a navigation bar, but I'm struggling to figure out where to place the necessary code. I attempted creatin ...

The code within a for loop may not function properly when placed within the ngOnInt() function

I am struggling with running a for loop inside ngOnInit in Angular. I have a service that has a method getAllNews() which returns an array, not an observable. Since I can't use the subscribe() method, I want to iterate over this array. When I console ...

The frontend is not triggering the Patch API call

I am having trouble with my http.patch request not being called to the backend. This issue only occurs when I try calling it from the frontend. Oddly enough, when I tested it in Postman, everything worked perfectly. Testing the backend on its own shows t ...

class-validator: ensures the correct number of digits are present in numeric values

Seeking assistance on validating the number of digits for numeric values using class-validator. Specifically, I want my entity to only accept numbers with 6 digits for a certain property. For example: const user1 = new User(); user1.code = 123456 // should ...

Trouble with running the "npm run tsc" command in Angular 2 beta

I'm facing an issue with compiling my Typescript using the command npm run ts. What's odd is that I can successfully compile and run it by running npm start. Below is the log: 0 info it worked if it ends with ok 1 verbose cli [ 'node' ...

Reversing a Firebase list in Angular 2 after inserting a new item: A step-by-step

I am currently looking for a solution to reverse my Firebase list in real-time. For instance: Let's say I have the following Firebase list: {apple, orange, banana}; == After reversing => {banana, orange, apple} If I were to add a new item (with ...

Remove the package from the @types folder within the node_modules directory

I currently have the 'mime' library in my node_modules directory and I am looking to completely remove it from my project, along with its @types files. The reason for this is that the old mime package is not functioning correctly for me, so I wan ...

Value is not defined when subscribing to subject in Event Bus Implementation

After creating an EventBusService and EventData, here is the implementation: export class EventData { public eventName: string; public event: any; constructor(eventName: string, event: any) { this.eventName = eventName; this.e ...