简体   繁体   English

我如何使矩形承诺连续出现?

[英]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. 我正在尝试通过Restangular,Angular-ui-utils进行带有后端验证的表单,但是我无法弄清楚如何在继续使用该函数之前强制Restangular的诺言完成,因此我的程序不断返回空值。

Here is my code 这是我的代码

HTML: HTML:

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

</form>
...

Javascript: 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. Rest Serice的输出确实很简单,如果名称不在数据库中,则为“ false”,否则为“ true”。

Since the call to validate unique name is async. 由于验证唯一名称的调用是异步的。 You cannot implement by returning checkResult . 您不能通过返回checkResult来实现。

ui-validate has i think support for promise. ui-validate我认为对诺言的支持。 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. 如果您使用的是ui-validate正确版本,则应该可以使用。 Check this commit https://github.com/angular-ui/angular-ui/issues/257 which adds support for promise in the api. 检查此提交https://github.com/angular-ui/angular-ui/issues/257 ,它会在api中添加对promise的支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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