ng-change 检测 ng 模型在选择框中为空或无效,然后选择第一个有效选项
ng-change detect if ng-model is null or invalid in select box and select first valid option
我有数百个这样的案例(所以修复必须是全局的,而不仅仅是这个特定的例子)
有很多这样的选择框:
<select ng-model="selectedItem">
<option ng-repeat="item in items | filter:attributes" value="{{item.id}}">{{item.name}}</option>
</select>
selectedItem
变量为 null(它将始终初始化为 null,在此特定情况下无法在控制器中更改)。
我想弄清楚的是一种全局监视视图中所有<select>
元素的方法,查看该<select>
框的ng-model
变量是否null
,如果null
将其设置为该选择框中的第一个有效选项,每当范围更改时,它都需要检查 ng-model 是否null
并自动选择第一个有效选项。
要意识到这一点的关键是,您可以定义多个具有相同名称的 Angular 指令,并且所有这些指令都将为匹配的元素运行。
这非常强大,因为它使您能够扩展内置指令或第三方指令等的功能。
使用它,我能够创建一个 select
指令,只要模型值为 null,它就会选择列表中的第一个有效选项。
但是,它不做的一件事是,如果您从列表中删除所选项目(它又恢复为空白),则可应对。但希望这足以让你开始。
var app = angular.module('stackoverflow', []);
app.controller('MainCtrl', function($scope) {
$scope.selectedItem = null;
$scope.items = [1, 2, 3, 4].map(function(id) {
return {
id: id,
visible: true,
text: 'Item ' + id
};
});
});
app.directive('select', function() {
return {
restrict: 'E',
require: '?ngModel',
link: function($scope, $elem, $attrs, ngModel) {
// don't do anything for selects without ng-model attribute
if (!ngModel) return;
// also allow specifying a special "no-default" attribute to opt out of this behaviour
if ($attrs.hasOwnProperty('noDefault')) return;
// watch the model value for null
var deregWatch = $scope.$watch(function() {
return ngModel.$modelValue;
}, function(modelValue) {
if (modelValue === null) {
// delay to allow the expressions to be interpolated correctly
setTimeout(function() {
// find the first option with valid text
var $options = $elem.find('option'),
$firstValidOption, optionText;
for (var i = 0, len = $options.length; i < len; i++) {
optionText = $options.eq(i).text();
if (optionText !== '' && !optionText.match(/^('?|{)/)) {
$firstValidOption = $options.eq(i);
break;
}
}
if ($firstValidOption) {
$firstValidOption.prop('selected', true);
ngModel.$setViewValue($firstValidOption.attr('value'));
// trigger a digest so Angular sees the change
$scope.$evalAsync();
}
}, 0);
}
});
// clean up in destroy method to prevent any memory leaks
var deregDestroy = $scope.$on('$destroy', function() {
deregWatch();
deregDestroy();
});
}
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<div ng-app="stackoverflow">
<div ng-controller="MainCtrl">
<select ng-model="selectedItem">
<option ng-repeat="item in items | filter:{visible:true} track by item.id" value="{{item.id}}">{{item.text}}</option>
</select>
<p>Visible items:</p>
<ul>
<li ng-repeat="item in items track by item.id">
<label>
<input type="checkbox" ng-model="item.visible">{{item.text}}
</label>
</li>
</ul>
</div>
</div>
相关文章:
- SlickGrid行选择无效
- 用户可以在AngularUI日期选择器中键入无效的日期
- 在jquery日期选择器中禁用特定日期无效
- 在索引更改中选择的下拉列表无效!!由于引导
- 带有 querySelectorAll 的无效选择器 :不带后代
- 谷歌图表:无效的列标签 |类别筛选器作为列选择器
- 选择第一个选项时使选择无效
- 引导轮播显示无效选择错误
- 选择更改对鼠标输出函数 JQUERY 无效
- ng-change 检测 ng 模型在选择框中为空或无效,然后选择第一个有效选项
- jQuery UI日期选择器getDate今天返回's的日期无效
- jQuery数据选择器解析日期引发无效的日期异常
- parsley.js选择列表值为零-无效
- Slickgrid复选框行选择无效
- htmlunit:指定了一个无效或非法的选择器
- 选择特殊字符无效
- "error": "无效来源"使用dropbox选择器
- 单击selectAll复选框后,“选择/取消选择”复选框无效
- d3嵌套选择-表追加在d3.v2中有效,但在d3.v3中无效
- 未捕获的错误:无效的选择器- #jqxCheckBox!请检查使用的“ID”或“CSS类名”是否正确