ng-change甚至有时使用选择标签也不起作用?



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-helper').on('select2:selecting', function(evt) {
console.log($('.code-helper').val());
previousValue = $('.code-helper').val();
});
});

我附有 ng 模型,但有时仍然没有调用 ng-change 函数,这只在这种情况下发生。

当检测到更改时:!globalEditor1.isClean() = true,则我正在尝试将selected value替换为以前的值。 这发生得很好。但是现在当我尝试从select标记更改值时,它不会触发ng-change事件。

斯菲德尔

点击这里

重现步骤:

请尝试以下操作:再次运行链接。 然后步

1(从下拉列表中选择"帐户处理器测试" 2( 从警报中选择确定 3( 选择"添加主要联系人",然后从警报中选择取消 4(现在保留以前的值。 5(现在再次选择"添加主要联系人"。

事件不会触发。

这有效:

ng-change="retrieveSelectedClass(selectedName, '{{selectedName}}')"

.JS

$scope.retrieveSelectedClass = function(newValue, oldValue) {
var oldValueSelected = {};
if (angular.isUndefined(oldValueSelected.id) && oldValue.indexOf('"id"') !== -1) {
oldValueSelected = JSON.parse(oldValue);
}
var possibleOldValues = $filter('filter')($scope.names, {
id: oldValueSelected.id
}, true);
oldValue = possibleOldValues[0];
}

最新更新