Search code examples
htmlangularjsangular-ui-bootstrap

ng-blur with uib-typeahead (ui-bootstrap)


   <div class="form-group">
       <label for="Skill1">Skill</label>
       <input type="text"
        ng-model="vm.skill1" 
        name="skill1"
        uib-typeahead="skill for skill in vm.skills | filter:$viewValue"
        class="form-control typeahead"
        ng-blur="vm.checkSkills(vm.skill1)"
        placeholder="" 
        required> 
   </div>

My environment is AngularJs, I'm trying to use ng-blur only when the user leave this field, however due to the typeahead, if I use the mouse to choose an option the typeahead suggest, the ng-blur "think" I left the field and activate the ng-blur just with the first letter I emtered How can I override this?


Solution

  • Instead of using ng-blur, required functionality can be achieved using

    typeahead-on-select="checkSkills($item,$model,$label)"

    HTML

    <input  type="text"  ng-model="skill1" uib-typeahead="skill for skill in skills | filter:$viewValue" class="form-control typeahead" typeahead-on-select="checkSkills($item,$model,$label)"
    

    JS

    $scope.checkSkills = function(item, model, label){ 
       if(model){
          //your validation logic goes here
         // console.log (" Check skill, correctly set ", (item), model , label);
       }
    
     }
    

    Plunker example : https://plnkr.co/edit/7N8TU2?p=preview