HTML
<select ng-model="selectedName" ng-change="retrieveSelectedClass()" ng-options="(item.name||item) group by item.groupName for item in names"
class="code-helper" id="code-helperId">
<option value="">Select Option</option>
</select>
JavaScript
var globalEditor1 = null;
var globalMergeEditor = null;
var widgets = [];
var timeout;
var app = angular.module('myApp', []);
var previousValue;
app.controller('OrderFormController', function($scope, $http) {
$scope.retrieveSelectedClass = function() {
$scope.isPaneShown = true;
if ($scope.selectedName === undefined) {
$scope.isPaneShown = false;
return;
}
if ($scope.selectedName.groupName === 'Create New') {
if (globalEditor1) {
if (!globalEditor1.isClean()) {
var r = confirm("You have unsaved changes, are you sure you want to proceed ?");
if (r != true) {
$('.code-helper').val(previousValue);
$scope.isPaneShown = false;
return;
}
}
}
$scope.isPaneShown = false;
} else {
if (globalEditor1) {
if (!globalEditor1.isClean()) {
var r = confirm("You have unsaved changes, are you sure you want to proceed ?");
if (r != true) {
$('.code-helper').val(previousValue);
$scope.isPaneShown = false;
return;
}
}
}
}
}
});
$(document).ready(function() {
$('.code-helper').select2({
placeholder: 'Select a command to begin'
});
$('.code-he pd crlper').on('select2:selecting', function(evt) {
console.log($('.code-helper').val());
previousValue = $('.code-helper').val();
});
});
I am having an issue with the ng-model and ng-change interactions. Despite being attached, sometimes the ng-change function does not get triggered under specific circumstances.
When the condition !globalEditor1.isClean() = true
is met, I attempt to replace the selected value
with the previous value. This process works correctly. However, when trying to change the value from the select
tag afterwards, the ng-change
event fails to fire.
jsfiddle
Take a look at the jsfiddle here
To reproduce the issue:
Follow these steps after running the provided link: 1) Choose "AccountProcessorTest" from the dropdown menu 2) Confirm the alert dialog 3) Select "AddPrimaryContact" and cancel the alert prompt 4) Notice that the previous value is retained 5) Try selecting "AddPrimaryContact" again
The ng-change event will not be triggered.