Giovanni Far Giovanni Far - 1 year ago 157
AngularJS Question

ngChange on input changed by a select

I have a simple select input field where the user can choose an option:

<select class="form-control" id="calc-masterbatchCode"
ng-options="masterbatch as masterbatch.code for masterbatch in masterbatches"

as you can see the
object is stored in a parent controller.

Then i have a disabled input field in the order to show the property of the

<input type="text" class="form-control" id="calc-masterbatchPercentage"

It works, but the problem is the

because it doesn't fire the
function when the user select another masterbatch.

The only way to fire that function is to type something manually into the
input field.

My goal is:

the user select a masterbatch, then he can see the masterbatch's property (the percentage) into the disabled input field and, when this one change, the
function is fired.

enter image description here

Answer Source

ngChange directive doesn't work on disable/readonly inputs, because it's only evaluated when a change in the input value causes a new value to be committed to the model. So, since you're changing the value programmatically and not typing anything to this model, it doesn't calls ngChange

Here is more one part extracted from docs:

    ngChange will not be evaluated:

  • if the value returned from the $parsers transformation pipeline has not changed
  • if the input has continued to be invalid since the model will stay null
  • if the model is changed programmatically and not by a change to the input value
  • So, to achieve what you want you must move your ng-change to your <select>.

    I hope it helps.

    Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download