angularjs - Angular JS Process form before submit -
i have scenario follows :
i have 1 searchbox , user can enter 2 types of queries
scenario 1 search starts i.e. a1234 scenario 2 search starts i.e. b1234
depending on 1 or 2 above want hit different webservice.
whats best way distinguish search on above input. when user clicks on ng-submit use regular expression ?
or should have scope variable on .watch() find out user typing ?
<form role="form" ng-submit="searchform.$valid && searchcode()" name="searchform" novalidate> <div class="input-group"> <input type="text" class="form-control" ng-model="searchquery" name="searchquery" required autocomplete="off" spellcheck="false" focus> <input type="submit" value="search" class="btn btn-default btn-sarch-bar"> </div> </form>
if search activated upon submit-button click, can handle login in submit function, , hit proper webservice accordingly. along these lines:
<form role="form" ng-submit="submit(searchform.$valid, data)" name="searchform" novalidate> <div class="input-group"> <input type="text" class="form-control" ng-model="data.searchquery" name="searchquery" required autocomplete="off" spellcheck="false" focus> <input type="submit" value="search" class="btn btn-default btn-sarch-bar"> </div> </form>
submit function:
$scope.submit = function(isvalid, data) { if(!isvalid) return; //check data.searchquery , access appropriate webservice here }
if you're using angularjs 1.3, , you'd perform search upon user interaction input, might interested in adding directive input element, , use $asyncvalidators
there's great tutorial yearofmoo regarding forms , validators using directives.
Comments
Post a Comment