angular.js - Angularjs1.5中如何返回一个空的Promise对象
世界只因有你
世界只因有你 2017-05-15 17:14:26
0
2
1213

在angular1.5.8中我在调用接口返回数据时,如果请求参数为空或undefined,则return,但是这个return返回的值位空没有.then,就报错了
事例代码:

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;
        })
    }
    

报错代码:

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)

我想在这边:

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

求问如何模拟一个空的promise对象?求解!谢谢!

世界只因有你
世界只因有你

全部回复(2)
大家讲道理

雷雷

黄舟

首先我这边用的是ng1+ts,需要注意先注入$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;

我找到了更好的方法

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!