简体   繁体   中英

How do I make restangular promise sequential?

I am trying to make a form with back end validation through Restangular, Angular-ui-utils but I can't figure out how to force Restangular promises to finish before continue on with the function and thus my program keep returning null values.

Here is my code

HTML:

...
<form> <input name="name" 
        ng-model="name" 
        ui-validate="{uniqueName: 'uniqueName($value)'}">
        Is name duplicated? {{form.name.$error.uniqueName}}</input>

</form>
...

Javascript:

...
function myCtrl($scope,Restangular) {
...
$scope.uniqueName(value) {
   var checkResult;
   Restangular.one('service').one('validate',value).get().then(function(result){
      checkResult = result;
   }
   return checkResult;
}
...
}

Rest Serice output are really straight forward, it's either 'false' if the name is not in the databse, and 'true' if it is.

Since the call to validate unique name is async. You cannot implement by returning checkResult .

ui-validate has i think support for promise. So you return something like

$scope.uniqueName(value) {
   return Restangular.one('service').one('validate',value).get();
}

which should be a promise i believe. If you are on correct version of ui-validate then it should work. Check this commit https://github.com/angular-ui/angular-ui/issues/257 which adds support for promise in the api.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM