[英]How to prevent user form ignoring suggestion in angularjs angular-ui-bootstrap uib-typeahead
我正在使用AngularJS v1.6.6和angular-ui-bootstrap版本:2.5.0创建自动完成字段。
一切正常,但是我需要一种方法来确保用户实际上从建议列表中选择了该选项。
这是我的代码:
HTML:
<div class='container-fluid typeahead-demo' ng-controller="TypeaheadCtrl">
<h4>How to prevent user from typing the whole word ignoring suggestions?</h4>
<div>Model:
<pre>{{selected | json}}</pre>
</div>
<form role="form" name="chooseStateForm" autocomplete="off" novalidate>
<div class="form-group required">
<div>
<label for="state" class="control-label col-sm-3">Choose a State:</label>
<input type="text"
class="form-control"
required
placeholder="Try typing the whole name of the state ignoring suggestion"
name="state"
ng-model="selected"
uib-typeahead="option as option.name for option in states | filter:{name: $viewValue}"
typeahead-min-length="1"
typeahead-no-results="noresults"
typeahead-show-hint="true"
>
</div>
<div ng-if="noresults">
<p>No match found!</p>
</div>
</div>
</form>
<br><br><br><br>
<div>
<button class="btn btn-primary" type="button" ng-click="$ctrl.ok()" ng-disabled="chooseStateForm.$invalid">OK</button>
<button class="btn btn-primary" type="button" ng-click="$ctrl.cancel()">Cancel</button>
</div>
JS:
angular.module('app', ['ui.bootstrap']);
angular.module('app').controller('TypeaheadCtrl', function($scope) {
$scope.selected = undefined;
$scope.states = [
{id: 1, name: 'Alabama'},
{id: 2, name: 'California'},
{id: 3, name: 'Delaware'},
{id: 4, name: 'Florida'},
{id: 5, name: 'Georgia'},
{id: 6, name: 'Hawaii'},
{id: 7, name: 'Idaho'},
{id: 8, name: 'Kansas'},
{id: 9, name: 'Louisiana'},
{id: 10, name: 'Maine'},
{id: 11, name: 'Nebraska'},
{id: 12, name: 'Ohio'},
{id: 13, name: 'Pennsylvania'},
{id: 14, name: 'Rhode Island'},
{id: 15, name: 'South Carolina'},
{id: 16, name: 'Tennessee'},
{id: 17, name: 'Utah'},
{id: 18, name: 'Vermont'},
{id: 19, name: 'Washington'}
];
});
看到这个jsfiddle,您将理解我的意思: http : //jsfiddle.net/elenat82/yhpbdvva/20/
例如,如果用户希望选择俄亥俄,由于只有4个字母,他会发现只键入“俄亥俄”比选择建议的选项容易。
但是,如果他从建议列表中进行选择,那么我的模型将成为一个字符串,而它却是一个对象。
是的,我在控制器中检查了模型的有效性,但是希望在用户提交表单之前完成模型,我想显示一条错误消息,向用户解释他做错了什么。
----------编辑----------
我找到了实现相同结果的另一种方法,但是使用了指令并扩展了$ validators对象。
这是更新的jsfiddle的链接: http : //jsfiddle.net/elenat82/fL5fw1up/2/
这是更新的代码:
HTML:
<div class='container-fluid typeahead-demo' ng-controller="TypeaheadCtrl">
<h4>How to prevent user from typing the whole word ignoring suggestions?</h4>
<div>Model:
<pre>{{selected | json}}</pre>
<div>Errors:
<pre>{{chooseStateForm.state.$error | json}}</pre>
</div>
<form role="form" name="chooseStateForm" autocomplete="off" novalidate>
<div class="form-group required">
<div>
<label for="state" class="control-label col-sm-3">Choose a State:</label>
<input type="text"
class="form-control"
required
placeholder="Try typing the whole name of the state ignoring suggestion"
name="state"
ng-model="selected"
uib-typeahead="option as option.name for option in states | filter:{name: $viewValue}"
typeahead-min-length="1"
typeahead-no-results="noresults"
typeahead-show-hint="true"
object
>
</div>
<div ng-if="noresults">
<p>No match found!</p>
</div>
</div>
</form>
<br><br><br><br>
<div>
<button class="btn btn-primary" type="button" ng-click="$ctrl.ok()" ng-disabled="chooseStateForm.$invalid">OK</button>
<button class="btn btn-primary" type="button" ng-click="$ctrl.cancel()">Cancel</button>
</div>
JS:
angular.module('app', ['ui.bootstrap']);
angular.module('app').controller('TypeaheadCtrl', function($scope) {
$scope.selected = undefined;
$scope.states = [
{id: 1, name: 'Alabama'},
{id: 2, name: 'California'},
{id: 3, name: 'Delaware'},
{id: 4, name: 'Florida'},
{id: 5, name: 'Georgia'},
{id: 6, name: 'Hawaii'},
{id: 7, name: 'Idaho'},
{id: 8, name: 'Kansas'},
{id: 9, name: 'Louisiana'},
{id: 10, name: 'Maine'},
{id: 11, name: 'Nebraska'},
{id: 12, name: 'Ohio'},
{id: 13, name: 'Pennsylvania'},
{id: 14, name: 'Rhode Island'},
{id: 15, name: 'South Carolina'},
{id: 16, name: 'Tennessee'},
{id: 17, name: 'Utah'},
{id: 18, name: 'Vermont'},
{id: 19, name: 'Washington'}
];
});
angular.module('app').directive('object', [function() {
return {
restrict: 'A',
scope: {},
require: 'ngModel',
link: function (scope, element, attrs, ngModel) {
ngModel.$validators.object = function(modelValue,viewValue){
if (angular.isObject(modelValue)) {
return true;
}
else {
return false;
}
};
}
};
}
]);
您可以为此验证添加方法,例如:
$scope.isSelected = function() {
return typeof $scope.selected == "object";
}
有一个名为typeahead-editable="false"
的属性。 如果将其设置为false,则不允许用户“不”选择某些内容,文本框将设置为空。
链接: https : //angular-ui.github.io/bootstrap/#!#typeahead
typeahead-editable $(默认值:true)-是否应将模型值限制为仅从弹出窗口中选择的值?
您将需要遍历数组,并检查所选值(ngModel)在该数组上是否存在。 您可以定义这样的函数并使用ngChange
调用它,该函数将在您更改值时运行
<input type="text"
class="form-control"
required
name="state"
ng-model="selected"
.....
ng-change="testIncluded(selected)">
在控制器内部,执行以下操作(使用Array.prototype.find )
$scope.testIncluded = function(value) {
let isSelectedFromStates = $scope.states.find((state) => {
return state.name === value;
})
/// do something ...
}
如果名称存在,则isSelectedFromStates
不会是不确定的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.