Is Angular's ngOnChanges failing to detect any changes?

Within one component, I have a dropdown list. Whenever the value of the dropdown changes, I am attempting to detect this change in value in another component. However, I am encountering an unusual issue. Sometimes, changing the dropdown value triggers the ngOnChanges function, while other times it does not. The reason for this inconsistency is unknown.

This represents the code:

AppComp

html

    <td width="20%" class="cursor">
      <select (change)="changedvalue($event)" class="form-control" name="level">
        <option  hidden selected> -- Select options -- </option>
        <option>Level 1</option>
        <option>Level 2</option>
      </select>
    </td>

   <td class="cursor">
     <app-other-comp [group]="group"></app-other-comp>
   </td>

ts

group: string;

changedvalue(event: Event) {
   const value = (<HTMLSelectElement>event.target).value;
   this.group = value;
   console.log('From AppComp' + this.group);
  }

OtherComp

ts

@Input() group: string;

ngOnChanges() {
    console.log('Changed : ' + this.group);
  }

As shown in the browser console, there are instances where the change is detected and others where it is not.

Change not detected:

https://i.stack.imgur.com/qcv6o.png

Change detected:

https://i.stack.imgur.com/8riJf.png

Answer №1

When the input is blurred, the change function is triggered. You can utilize ngModel and ngModelChange like so:

<td width="20%" class="cursor">
  <select (ngModelChange)="changedvalue($event)" [(ngModel)]="value"  class="form-control" name="level">
    <option hidden selected> -- Select options -- </option>
    <option>Level 1</option>
    <option>Level 2</option>
  </select>
</td>

<td class="cursor">
 <app-other-comp [group]="group"></app-other-comp>
</td>

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

Why are cloned jQuery elements triggering events on the parent <div> and not the child <div>?

Currently, I am working on a tool that includes dynamic input fields for user input. To create different sections, I have cloned some divs from the code successfully. However, I am facing an issue where the events attached to the parent div are triggered e ...

In what situations might a finally block fail to execute?

Are there any circumstances where the code in a finally block may not be reached, aside from the usual suspects like process exit(), termination signal, or hardware failures? In this TypeScript code snippet that usually runs smoothly in node.js, occasiona ...

How can I ensure I am receiving real-time updates from a Resolver Service by subscribing and staying in sync with the

How can I effectively implement this code without encountering an error? "Property 'resolve' in type 'DocumentaryResolverService' is not assignable to the same property in base type 'Resolve'." import { Documentary } from ...

Losing $scope reference when incorporating a directive within the controller scope

I have a scenario in my code where I am using two directives. One directive is within the controller scope, while the other directive is not located in the html section. <div ng-show="isStore==true"> <packgroup-directive> ...

Here are some steps to turn off browser autocomplete for a p-inputMask field

I need help in disabling the browser autocomplete on a field that uses p-inputMask. I have tried using autocomplete="nope" on other fields and it works perfectly, but for this specific case, it doesn't seem to work. Can anyone point out what I might b ...

Is @babel the solution to transpile Array.prototype.flat?

I accidentally caused a backward compatibility issue in my React application by utilizing Array.prototype.flat. I was quite surprised that this problem persisted even after transpiling - I had assumed it would generate es2015-compatible code. Is there a w ...

Is it possible to use async/await together with forEach in JavaScript?

Within my array of objects containing user information and emails, I aim to send emails using AWS SES. To accomplish this, I must decide between utilizing await or normal .then. Preferably, I would like to use await within a forEach loop. Is it feasible to ...

How come the hidden container does not reappear after I click on it?

I'm having an issue with a hidden container that holds comments. Inside the container, there is a <div> element with a <p> tag that says "Show all comments". When I click on this element, it successfully displays the comments. However, cli ...

I am interested in running JavaScript code on a page that has been loaded through AJAX

I am struggling to execute the Javascript loaded within an ajax page. Here is the link to my loaded ajax page: https://jsfiddle.net/4dsbry7j/ JS : var xmlhttp = new XMLHttpRequest(); var url = "http://localhost/ajax/find2.php"; xmlhttp.onreadystatechang ...

Are you struggling with perplexing TypeScript error messages caused by a hyphen in the package name?

After creating a JavaScript/TypeScript library, my goal is for it to function as: A global variable when called from either JavaScript or TypeScript Accessible via RequireJS when called from either JavaScript or TypeScript Complete unit test coverage Th ...

Creating a personalized stepper component (using custom icons) using HTML

Seeking guidance on how to achieve a design similar to the image below using HTML and CSS. After conducting extensive research, I have not been able to find a suitable solution. I attempted to modify this answer to fit my requirements, but it did not prod ...

AngularJS App disrupted due to Direct Link to URL containing route parameters

Having an issue with direct links to pages containing a parameter. While links from the page itself work, accessing the page directly or refreshing it causes it to break and not load anything. This problem is occurring within a blog application I am develo ...

Creating a Node.js asynchronous setup function

I'm in the process of transitioning from Nodejs v12 to v14 and I've noticed that v14 no longer waits for the setup function to resolve. My setup involves Nodejs combined with Express. Here's a simplified version of my code: setup().then(cont ...

Using Ionic2 to connect with PHP server APIs

I am fairly new to working with ionic, although I have a basic understanding of angular2. My goal is to access an API created in PHP using the combination of ionic2 and angular2. feed.php header('Access-Control-Allow-Origin: *'); $db_ ...

Can you determine the size of an unknown array in TypeScript?

Currently diving into TypeScript and tackling some TDD challenges. Within my model file, I'm working with a property named 'innovatorQuotes' that should be an array containing strings with a fixed length of 3. I'm struggling to nail dow ...

What is the best way to retrieve every single element stored in an Object?

On a particular page, users can view the detailed information of their loans. I have implemented a decorator that retrieves values using the get() method. Specifically, there is a section for partial repayments which displays individual payment items, as d ...

Angular - Replicated Side Navigation

I am a beginner in Angular and I am using Angular 10 to design a simple page with a side bar and footer using Angular Material components. I am encountering an issue with displaying the left side navigation correctly as it is currently being duplicated. Be ...

What is the best way to execute a randomly chosen function in JavaScript?

I need help running a random function, but I'm struggling to get it right. Here's what I have so far: <script> function randomFrom(array) { return array[Math.floor(Math.random() * array.length)]; } function randomchords(){ randomFrom ...

What is the process for updating parameters before finalizing a route in Vue.js?

I have set up a route with a value parameter: routes.push({ name: 'test', path: '/test/:value', component: resolve(__dirname, 'src/pages/test.vue'), }); Now, I want to modify the route so that it also include ...

Just starting out with jQuery: seeking advice on a user-friendly slideshow plugin, any tips on troubleshooting?

I am currently trying to incorporate a basic jquery slideshow plugin, but I seem to be encountering some difficulties. The documentation mentions the need to install 'grunt' and 'node dependencies', which is confusing to me as I am new ...