recherche

Maison  >  Questions et réponses  >  le corps du texte

angulaire.js - Comment renvoyer un objet Promise vide dans Angularjs1.5

Dans angulaire 1.5.8, lorsque j'appelle l'interface pour renvoyer des données, si le paramètre de requête est vide ou indéfini, alors retournez, mais la valeur renvoyée par ce retour est vide et n'a pas .then, et une erreur est signalée
Code du cas :

onGoodsNameAutoComplate = (value: string) => {
        if (!this.clientId || !this.$scope.goodsTypeId) {
            return;//这边返回就是空的,没有.then,则会报错
        };
        return this.goodsService.getList(this.clientId, value, this.$scope.goodsTypeId, 0, 5).then(res => {
            return res.data.list;
        })
    }
    

Code d'erreur :

TypeError: Cannot read property 'then' of undefined
    at T (angucomplete-alt.min.js:2)
    at $ (angucomplete-alt.min.js:2)
    at m.b.onFocusHandler (angucomplete-alt.min.js:2)
    at fn (eval at compile (angular.js:14817), <anonymous>:4:233)
    at b (angular.js:15906)
    at e (angular.js:25885)
    at m.$eval (angular.js:17682)
    at m.$apply (angular.js:17782)
    at HTMLInputElement.<anonymous> (angular.js:25890)
    at HTMLInputElement.dispatch (jquery.min.js:3)

Je veux être ici :

 if (!this.clientId || !this.$scope.goodsTypeId) {
                return;//这边模拟返回一个空的promise对象,就不会报错了。。。
            };

Comment simuler un objet de promesse vide ? Trouvez la solution ! Merci!

世界只因有你世界只因有你2856 Il y a quelques jours1309

répondre à tous(2)je répondrai

  • 大家讲道理

    大家讲道理2017-05-15 17:16:26

    onGoodsNameAutoComplate = (value: string) => {
      return $q(function (resolve, reject) {
        if (!this.clientId || !this.$scope.goodsTypeId) {
          reject('xxx不能为空')
          return
        }
        this.goodsService.getList(this.clientId, value, this.$scope.goodsTypeId, 0, 5).then(
          res => {
            resolve(res.data.list)
          },
          () => reject('无法获取到正确的数据'))
      }
    }

    répondre
    0
  • 黄舟

    黄舟2017-05-15 17:16:26

    Tout d'abord, j'utilise ng1+ts ici, je dois d'abord faire attention à l'injection de $q

    .
    private $q: ng.IQService
    angular.module("app").controller("$q",demoController]);
     var deferred = this.$q.defer();
            if (!this.clientId || !this.$scope.goodsTypeId) {
                // deferred.resolve("well done!");
            } else {
                return this.goodsService.getList(this.clientId, value, this.$scope.goodsTypeId, 0, 5).then(res => {
                    return res.data.list;
                })
            }
            return deferred.promise;

    J'ai trouvé une meilleure façon

    répondre
    0
  • Annulerrépondre