Angularjs Select Doesn't Error When Model Value Not In Options August 21, 2024 Post a Comment Angular 1.4.8. I have this markup for 2-letter US state codes: Solution 1: One thing you can do to trigger errors on the ngModel directive is to add a custom validator to the ngModel's $validators. The ngModel's value is passed through the $validators whenever it changes, and an error is raised if the validator returns false. An example implementation is to add a custom directive to the model's element, and define the validator inside that directive: Baca JugaUpdating Multiple Select Boxes DynamicallyHow Can I Add An Option In The Beginning?Drop Down Menu For Selecting Country Then StatesHere's a plunkr with a working example: http://plnkr.co/edit/m6OygVR2GyMOXTTVTuhf?p=preview// markup <select name="licenseState" id="licenseState"class="form-control" required ng-model="student.License.State" ng-options="key as value for (key, value) in licenseFormats" check-state license-formats="licenseFormats"> // in the controller $scope.licenseFormats = { 'OR': 'Oregon', 'WA': 'Washington', }; // the directive app.directive('checkState', function() { return { scope: { licenseFormats: '=' }, restrict: 'A', require: 'ngModel', link: function(scope, element, attributes, ngModel) { // defining the validator here ngModel.$validators.state = function(modelValue) { returnObject.keys(scope.licenseFormats).indexOf(modelValue) > -1; } } } }); Copy Share You may like these postsGoogle Bot Crawling On Angularjs Site With Html5 Mode RoutesSave Checkbox Value With Javascript The Easy Way?How To Use Ckeditor In An Angular Js Web App?Angular-ui-router With Html5mode Refresh Page Issue Post a Comment for "Angularjs Select Doesn't Error When Model Value Not In Options"
Post a Comment for "Angularjs Select Doesn't Error When Model Value Not In Options"