Angularjs select2下拉验证

时间:2018-01-31 05:53:03

标签: angularjs

我正在使用select2插件。我想在用户忘记选择提交时的下拉值时向用户显示错误。但是发生了什么意味着它正确地显示了错误消息,但是在选择了下拉值后显示错误消息。

我的代码是,

<select name="books" ng-model="books" wb-select2 initial-select-key="authour" target-id="books" ng-options="book.authour for book in books track by book.id" class="select2" required="true">

1 个答案:

答案 0 :(得分:0)

您可以将其包装在表单标记中,并在其上使用表单验证,如下所示:

<强> HTML:

<form name="formName" ng-submit="formName.$valid&&accountSave()" novalidate>
    <select name="books" ng-model="books" wb-select2 initial-select-key="authour" target-id="books" ng-options="book.authour for book in books track by book.id" class="select2" required="true">
    <p ng-if="formName.$submitted&&formName.books.$error.required" class="error">Books is required</p>
</form>

<强> JS:

$scope.accountSave=function(){
  //post form details to server
}

了解详情https://docs.angularjs.org/api/ng/directive/form