2

Is there a way to cancel ng-change event?

Actually I want to have an option in a select box but do not want user to select it but want to give a warning message and then make the previous option selected.

For that I think the best way should be if I can cancel the ng-change event in some way so it does not make any effect, something similar like when we return false in html onchange event it does not let you change the option.

I also tried to store the previous value in someway and then set that value in ng-change event but then found that ng-change doesn't pass event variable and so I cannot find the element triggering the event.

Any idea?

Edit

Something like this

<select ng-change="DeliveryMethodChange(item)" ng-model="item.DeliveryMethodId" ng-options="method.Key as method.Value for method in deliveryMethods">
         <option value="">(Please Select)</option>
</select>

Script

$scope.DeliveryMethodChange = function(item) {

    if (item.DeliveryMethodId.toUpperCase() == 'SH') {
          // here I want to reset the value to the previously selected value
          alert('You cannot select this delivery method');
          return false;
    }

    // rest of the code

};
Pawan Nogariya
  • 8,330
  • 12
  • 52
  • 105
  • Can you share some of your code so I can help? – itamar May 30 '16 at 11:30
  • Actually my problem is not code specific, it is general that I want to cancel the `ng-change` event in someway – Pawan Nogariya May 30 '16 at 11:31
  • Why don't you just set some logic inside event handler instead? It really looks like a XY problem. You'd have better to provide some minimalistic sample replicating your issue and clearly explain your expected behaviour – A. Wolff May 30 '16 at 11:38
  • I did, but I am not finding the way to reset the value to its old value. For that I need `select` reference inside `ng-change` as well as the last selected value. – Pawan Nogariya May 30 '16 at 11:40
  • Try using a watch on the model. There you have new and old value. – charlietfl May 30 '16 at 11:43
  • @charlietfl - My select box is inside `ng-repeat` – Pawan Nogariya May 30 '16 at 11:55
  • This whole idea doesn't seem like a very good UX. If I select something in a ` – charlietfl May 30 '16 at 12:07
  • I agree, but actually the business requirement is to show the value but don't let the user select it and when they try to select it give them the warning that because of the previously selected values in the form you cannot select this value. – Pawan Nogariya May 30 '16 at 12:13

2 Answers2

0

It's way late, but this answer answers the question. Essentially you can you {{value}} to refer to the current value, so pass this as an argument to your change function, and revert the field if you don't like what you did.

Angularjs - How to cancel change event on dropdown when the confirm dialog is false?

awiebe
  • 3,758
  • 4
  • 22
  • 33
-1

It could be the answer;

$scope.DeliveryMethodChange = function(item) {

     $scope.$watch('DeliveryMethodId', function (newVal, oldVal) {
                    if (newVal == 'SH') {
                        item.DeliveryMethodId = oldVal;
                    }
                });
    }
Egomen
  • 93
  • 3
  • 11