我是 angularjs 的新手。我的目标很简单。我想进行 ajax 调用以获取数据,完成后,我想进行第二次调用以获取依赖于第一组信息的另一组数据。
我正在尝试利用 promise 机制来做到这一点,这样我就可以利用链接而不是嵌套的 ajax 调用,并更好地保留拥有独立功能的能力,我可以根据需要将它们结合在一起。
我的代码类似于以下内容:
var promiseGetWorkTypes = function ($q, $scope, $http) {
console.log("promiseGetWorkTypes");
return $q(function (resolve, reject) {
$http({
method: 'GET',
url: '/WorkTypes'
}).then(
function (payload) {
console.log("Got workttypegroups")
console.log(payload);
$scope.WorkTypeGroups = payload.data;
console.log("End of worktypegroups");
resolve(payload);
},
function (payload) {
reject(payload);
});
});
};
var promiseGetRecentActivities = function ($q, $scope, $http) {
console.log("promiseGetRecentActivities");
return $q(function (resolve, reject) {
$http({
method: 'GET',
url: '/RecentHistory'
}).then(
function (payload) {
$scope.RecentActivities = payload.data;
resolve(payload);
},
// data contains the response
// status is the HTTP status
// headers is the header getter function
// config is the object that was used to create the HTTP request
function (payload) {
reject(payload);
});
});
};
var index = angular.module("index", []);
index
.controller('EntitiesController', function ($scope, $http, $timeout, $q) {
promiseGetWorkTypes($q, $http, $scope)
.then(promiseGetRecentActivities($q, $http, $scope));
}
但是,当我查看我的调试控制台时,我发现在调用“promiseGetWorkTypes”的 Ajax 处理之前开始调用“promiseGetRecentActivities”。
我在这里遗漏了什么或做错了什么?
最佳答案
当你写作时
promiseGetWorkTypes($q, $http, $scope).then(promiseGetRecentActivities($q, $http, $scope));
promiseGetActivites 在评估此行时被调用。您应该能够将对 promiseGetActivities 的调用包装在另一个函数中以延迟调用,直到第一个 promise 已解决以使调用按顺序运行:
promiseGetWorkTypes($q, $http, $scope).then(function() {
promiseGetRecentActivities($q, $http, $scope);
});
原因与 then 中发生的事情无关,而是由于 Javascript 语法。以下内容:
myFunc1(myFunc2());
将调用 myFunc2() 的结果传递给 myFunc1,不是对 myFunc2 函数的引用.所以从逻辑上讲,myFunc2 必须在 myFunc1 之前运行。如果你写了
myFunc1(myFunc2);
然后 myFunc1 将收到对 myFunc2 的引用,因此 myFunc1 将在 myFunc2 之前运行(并且在事实上,myFunc2 只有在 myFunc1 中的某处有调用它的代码时才会运行。
内联/匿名定义函数不会改变这种行为。要将匿名函数的结果传递给另一个函数,您可以执行以下操作
myFunc1((function() {
return 'something';
})());
这将首先评估匿名函数,作为其返回值,'something' 将传递给 myFunc1。要传递对匿名函数的引用,您可以执行以下操作:
myFunc1(function() {
return 'something';
});
然后它将取决于 myFunc1 是否会调用传递给它的函数。
回到你的问题,你的代码:
promiseGetWorkTypes($q, $http, $scope).then(promiseGetRecentActivities($q, $http, $scope));
正在将 promiseGetRecentActivities($q, $http, $scope) 的结果传递给 then,因此它必须在 then 之前运行运行,因此当然不会等待 promiseGetWorkTypes 的 promise 得到解决。您似乎想要的是向它传递一个函数,该函数在调用时运行 promiseGetRecentActivities($q, $http, $scope),这就是
promiseGetWorkTypes($q, $http, $scope).then(function() {
promiseGetRecentActivities($q, $http, $scope);
});
会。
作为旁注,将 $q、$http 等传递给各种函数似乎有点不寻常/过于复杂,但我认为可能超出了这个问题的范围要通过替代方案。
关于javascript - $http promise 链以错误的顺序运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28390370/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul
Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file