承诺创建自定义失败不是Angularjs函数错误吗? [英] Promise create custom fail is not a function Angularjs error?

查看:77
本文介绍了承诺创建自定义失败不是Angularjs函数错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我正在尝试使用promise在angularjs中创建html5目录函数. 但它显示错误,因为未定义的承诺.

As i am trying to use promise to create html5 directory functions in angularjs. but it shows error as promise not defined.

角度文件:-

$scope.createDirectory = function(dirName,dirLocation){
        fileManager.createDirectory(dirName,dirLocation)
        .then(function(data){
            console.log(data, "dir created");
        }).fail(function(err){
            console.log(err,"dir err while creating");
        });
    };

JS文件:-

var fileManager = {
createDirectory: function(directoryName,dirLocation){
                    var makePromise = new Promise(function(resolve, reject){
                        dirLocation.getDirectory(directoryName, {create: true, exclusive: false}, function(data){
                            resolve(data);
                        }, function(error){
                            reject(error);
                        });
                    });
                    return makePromise;
                }
}

TypeError: fileManager.createDirectory(...).then(...).fail is not a function
at h.$scope.createDirectory (app.js:60)
at angular.min.js:196
at f (angular.min.js:224)
at h.$eval (angular.min.js:123)
at h.$apply (angular.min.js:123)
at HTMLButtonElement.<anonymous> (angular.min.js:224)
at HTMLButtonElement.c (angular.min.js:32)

如何正确创建承诺并将其绑定到角度函数. 我已经使用catch而不是失败了.如何为所有失败功能创建自定义错误回调,例如

how to create promise properly and bind it to angular function. I've used catch instead of fail it worked. how to create custom error callback to all fail function like this

推荐答案

promise中有错字,应该为new Promise.尽管我建议您使用$q而不是Promise.

There is typo in promise, It should be new Promise. Though I would suggest you to use $q instead of Promise.

使用$q而不是Promise对象的优点是,您编写的代码将是有角度的上下文&您无需手动运行摘要循环.仿佛您使用Promise一样,则需要手动运行摘要循环(因为Promise是本机异步JS函数,被认为是超出角度的世界).

The advantage of using $q instead of Promise object is, you code will be angular context & you don't need to care to run digest cycle manually. Where as if you use Promise then you need to run digest cycle manually(as Promise would be native asynchronous JS function, considered to be outside world of angular).

createDirectory: function(directoryName, dirLocation) {
    var makePromise = $q(function(resolve, reject) {
        dirLocation.getDirectory(directoryName, {
            create: true,
            exclusive: false
        }, function(data) {
            resolve(data);
        }, function(error) {
            reject(error);
        });
    });
    return makePromise;
};

更新

.fail函数在$q对象上不可用,您需要将fileManager.createDirectory函数代码调用更改为以下内容.

.fail function isn't available on $q object, you need to change your fileManager.createDirectory function code call to below.

$scope.createDirectory = function(dirName,dirLocation){
    fileManager.createDirectory(dirName,dirLocation)
    .then(function(data){ //success callback
        console.log(data, "dir created");
    }, function(err){ //error callback
        console.log(err,"dir err while creating");
    });
};

这篇关于承诺创建自定义失败不是Angularjs函数错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆