Select doesn´t recognise ng-change

I have a "select" using ng-options. It has 4 "periods". After an event is triggered, this happens:

if ($scope.periods.length <= 4) {
  $scope.periods.push({
    IdPeriod: 6,
    Description: "PERSONALIZADA"
  });
}

setTimeout(() => {
  sel.options[sel.options.length - 1].selected = true;
}, 100);

I'm creating a new option, that shouldn't be showed from start. After it, with a short timeout (sel.options[5] seems undefined inmediatly after creating it), I'm selecting it to be showed in the "select".

The thing is that, if I trigger the event that creates that option, and re-select the option that was selected before creating the new one, it won't call the method assigned to ng-change.

Upvotes: 1

Views: 69

Answers (1)

georgeawg
georgeawg

Reputation: 48968

ERRONEOUS

setTimeout(() => {
  sel.options[sel.options.length - 1].selected = true;
}, 100);

BETTER

$timeout(() => {
  sel.options[sel.options.length - 1].selected = true;
}, 100);

Events from setTimeout are not integrated with the AngularJS framework.

AngularJS modifies the normal JavaScript flow by providing its own event processing loop. This splits the JavaScript into classical and AngularJS execution context. Only operations which are applied in the AngularJS execution context will benefit from AngularJS data-binding, exception handling, property watching, etc.

For more information, see

Upvotes: 1

Related Questions