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

查看:25
本文介绍了Promise 创建自定义失败不是函数 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)

如何正确创建 promise 并将其绑定到 angular 函数.我使用了 catch 而不是 fail 它起作用了.如何为所有失败函数创建自定义错误回调,例如 这个

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 函数,被认为是 angular 的外部世界).

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

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

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