相关疑难解决方法(0)

获得Angular的状态延期?

随着jQuery延迟,我以前能够像这样检查当前状态:

var defer = $.Deferred();
defer.state();  //Returns the state of the deferred, eg 'resolved'
Run Code Online (Sandbox Code Playgroud)

Angular deferreds有没有办法做同样的事情?(甚至更好的承诺)

jquery promise angularjs angular-promise

39
推荐指数
2
解决办法
4万
查看次数

获取$ http调用的完整调用堆栈跟踪

假设有人在一个文件中编写了一个这样的方法,该文件app.js试图通过一个不存在的url来执行XHR请求:

app.controller('MainCtrl', function($scope,$http) {
  $scope.send = function() {
    $http.get('http://run.plnkr.co/thisIs404');
  };
});
Run Code Online (Sandbox Code Playgroud)

我可以http://run.plnkr.co/thisis404在控制台和网络面板中看到有关URL的错误:

控制台错误

要调试这个我想快速找到在源中进行此XHR调用的位置(即查找app.js文件):

所以我启用了chrome dev工具:

  • 调用堆栈中的异步调试
  • 调试任何XHR

调试器实际上在XHR请求上停止,但调用堆栈仅显示对angular.js"核心"文件的引用 :没有对app.js 任何地方的引用.

谷歌Chrome调用堆栈

我尝试使用铬36和铬35.只有解决方案:在整个代码库中搜索错误的URL(在某些情况下可能很难).

  • 是不是异步调试模式应该指向app.js堆栈中的somwhere?
  • 有没有办法app.js从控制台错误轻松追踪此文件?

使用vanilla XHR请求(即没有角度),XHR调试调用堆栈显示XHR调用app.js(在这种情况下更容易调试):

在此输入图像描述

这里有完整的例子:http://plnkr.co/edit/lnCRpv?p = preview

[编辑]我被问到:Angular.js在我的测试中没有缩小.

javascript ajax asynchronous google-chrome-devtools angularjs

18
推荐指数
1
解决办法
5762
查看次数

typescript 2.1 with async/await为angularjs生成ES5/ES3目标

我试图async/await在一个角度1.5.5项目中使用.

鉴于这种服务方法

    getDocumentTypes(): angular.IPromise<DocumentType[]> {
        var url = "api/document/types";
        this.$log.log(url);
        return this.$http.get(url).then(_ => _.data);
    }
Run Code Online (Sandbox Code Playgroud)

我正在尝试创建async / await该方法的一个版本.

    async getDocTypes(): angular.IPromise<DocumentType[]> {
                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    }}
Run Code Online (Sandbox Code Playgroud)

Intellisense显示错误:TS1055 类型'angular.IPromise'不是有效的异步函数返回类型,ES5/ES3因为它不引用与Promise兼容的构造函数值.

有没有使用正确的方式角承诺打字稿2.1async/ await

javascript interface promise async-await typescript

12
推荐指数
1
解决办法
2572
查看次数

AngularJS : $q -> deferred API order of things (lifecycle) AND who invokes digest?

The $q service is very powerful in angularjs and make our life easier with asynchronous code.

I am new to angular but using deferred API is not very new to me. I must say that I completely ok with the How to use part of documentation + there are very useful links for that within the docs + I checked out the source either.

My question is more about the under the hood parts of deferred and promise API objects …

promise deferred angularjs angularjs-scope angular-promise

9
推荐指数
2
解决办法
4664
查看次数

在AngularJS的承诺中使用它

是否有最佳实践解决方案能够在承诺中使用?在jQuery中,我可以绑定我的对象以在我的promise/callback中使用它 - 但是在angularJS中?有最佳实践解决方案吗?"var service = this;"的方式 我不喜欢......

app.service('exampleService', ['Restangular', function(Restangular) {
    this._myVariable = null;

    this.myFunction = function() {
        Restangular.one('me').get().then(function(response) {
            this._myVariable = true; // undefined
        });
    }
}];
Run Code Online (Sandbox Code Playgroud)

这个问题有解决方案吗?如何在承诺范围内从我的服务中获取成员或方法?

先感谢您.

bind this promise

9
推荐指数
1
解决办法
5013
查看次数

避免忘记承诺回报

当我使用promises来表示作业之间的依赖关系时,解析后的值变得不重要,我可能会忘记某个地方的返回.例:

startSomething().then(function() {
  Q.all(tasks.map(function(task) { return task.startIt(); }))
}).then(collectOutput).done();
Run Code Online (Sandbox Code Playgroud)

这里Q.all返回一个承诺,我本应该返回.不这样做意味着collectOutput在调用时,所有任务都已启动,但无法保证它们已完成.

这种错误导致竞争条件,并且可能极难再现和追踪.所以我想知道,是否有一些工具可以帮助检测并避免这种问题?也许有些promise库会在一个函数返回undefined时发出警告?或者检测没有听众的承诺,Bluebird对未处理拒绝的方式呢?

javascript promise q

6
推荐指数
1
解决办法
319
查看次数

是否有工具从代码生成流程图?

我刚加入了一个用Angular JS制作的项目,它有很多嵌套的承诺.有些任务似乎没有以正确的顺序发生,这导致某些对象在某些时候被覆盖.我需要检查如何/何时调用promises,首先返回,等等.所有这些承诺发生了什么的流程图woudl帮助了我很多,不幸的是我需要花费相当长的时间来手动完成它,所以我想知道是否有一种自动化工具.我一直在谷歌上看,但找不到那样的东西.欢迎任何关于如何跟踪承诺电话/回复的提示!

javascript tree flowchart promise angularjs

5
推荐指数
1
解决办法
1156
查看次数