如何在async forEach循环后添加回调函数?
这是一些更好的背景:
$scope.getAlbums = function(user, callback) {
$scope.albumsList.forEach(function (obj, i) {
$scope.getAlbum(user, obj.id, function(value){
$scope.albums.push(value);
});
});
// callback(); ???
};
$scope.getAlbums('guy123', function(){
// forEach loop is all done!
console.log($scope.albums)
});
Run Code Online (Sandbox Code Playgroud)
控制器:
.controller('Filters', ['$scope','Imgur', function($scope, Imgur) {
$scope.getAlbum = function(user, id, callback) {
Imgur.album.get({user:user, id:id},
function(value) {
return callback(value.data);
}
);
}
$scope.getAlbums = function(user, callback) {
// Callback function at end of this forEach loop?
// Request is async...
$scope.albumsList.forEach(function (obj, i) {
$scope.getAlbum(user, obj.id, function(value){
$scope.albums.push(value);
}); …Run Code Online (Sandbox Code Playgroud) 假设我有一个RESTful端点,它接受一系列方面来查询数据.以下是一些例子:
example.com/search?type=Doctor&location=Boston,MA&radius=2
example.com/search?type=Facility&location=Wayne,NJ&radius=3&gender=f
example.com/search?type=Doctor&location=Patterson,NJ
Run Code Online (Sandbox Code Playgroud)
我的模块接受查询对象来执行搜索:
console.log(query);
{
type:'Doctor',
location:'Boston,MA',
radius:'2'
}
$scope.getSearch = function(query){
var search = $search.search(query);
search.getResults(function(results){
$scope.results = results;
});
}
Run Code Online (Sandbox Code Playgroud)
这些方面通过Web表单中的本地模型传递:
<input type="text" ng-model="query.location"/>
<input type="text" ng-model="query.radius"/>
<button type="button" ng-click="getSearch(query)">Search</button>
Run Code Online (Sandbox Code Playgroud)
在getResults函数的成功回调中,我试图将查询参数附加到URL,如上例所示:
$scope.getSearch = function(query){
var search = $search.search(query);
search.getResults(function(results){
$scope.results = results;
search.appendQueryToURL(query);
});
}
Run Code Online (Sandbox Code Playgroud)
如何在AngularJS中动态追加URL参数?
要查找设备的最大视口高度,包括空间,address bar以便我们可以动态调整min-body的大小并推送我们的内容.
移动浏览器以不同方式处理方向状态,并更改方向更改的DOM属性.
使用JavaScript检测浏览器中Android手机的旋转
使用Android手机,screen.width或者screen.height在设备旋转时也进行更新.
|==============================================================================|
| Device | Events Fired | orientation | innerWidth | screen.width |
|==============================================================================|
| iPad 2 | resize | 0 | 1024 | 768 |
| (to landscape) | orientationchange | 90 | 1024 | 768 |
|----------------+-------------------+-------------+------------+--------------|
| iPad 2 | resize | 90 | 768 | 768 |
| (to portrait) | orientationchange | 0 | 768 | 768 |
|----------------+-------------------+-------------+------------+--------------|
| iPhone 4 …Run Code Online (Sandbox Code Playgroud) 假设我有一个堆叠的文章:
<div ng-repeat="article in articles">
<h1> {{article.title}} </h1>
<p> {{article.body}} </p>
</div>
Run Code Online (Sandbox Code Playgroud)
滚动浏览每篇文章时:
<div scroll="atNewArticle(article)" ng-repeat="article in articles">
...
</div>
Run Code Online (Sandbox Code Playgroud)
我想执行一个函数:
.controller('AppCtrl', ['$scope', function($scope) {
$scope.atNewArticle = function(){
console.log(article);
}
}])
Run Code Online (Sandbox Code Playgroud)
我很难找到正确的方法,因为我不确定是否应该将滚动事件绑定到窗口或检测指令元素本身的偏移量.
这是我到目前为止所尝试的:http: //jsfiddle.net/6VFJs/1/
如何迭代Z中的每一行,其中Z是2*m矩阵:
6.1101,17.592
5.5277,9.1302
8.5186,13.662
Run Code Online (Sandbox Code Playgroud)
如何访问此循环中的每个Z(i)(j)?
例如:
for i = z
fprintf('Iterating over row: '+ i);
disp (i:1);
disp (i:2);
end
Run Code Online (Sandbox Code Playgroud)
输出:
Iterating over row: 1
6.1101
17.592
Iterating over row: 2
5.5277
9.1302
Iterating over row: 3
8.5186
13.662
Run Code Online (Sandbox Code Playgroud) 假设我有一个stuff模块,我想注入myAppconfig:
angular.module('myApp', ['stuff']).
config([function() {
}]);
Run Code Online (Sandbox Code Playgroud)
有两个子模块:
angular.module("stuff", ["stuff.thing1","stuff.thing2"]);
Run Code Online (Sandbox Code Playgroud)
这是第一个:
angular.module('stuff.thing1', []).provider("$thing1", function(){
var globalOptions = {};
this.options = function(value){
globalOptions = value;
};
this.$get = ['$http',function ($http) {
function Thing1(opts) {
var self = this, options = this.options = angular.extend({}, globalOptions, opts);
}
Thing1.prototype.getOptions = function(){
console.log(this.options.apiKey);
};
return {
thing1: function(opts){
return new Thing1(opts);
}
};
}];
});
Run Code Online (Sandbox Code Playgroud)
第二个是相同的,例如:
angular.module('stuff.thing2', []).provider("$thing2", function(){
var globalOptions = {};
this.options = function(value){
globalOptions = value;
};
this.$get …Run Code Online (Sandbox Code Playgroud) Daniel Collicott:
Run Code Online (Sandbox Code Playgroud)
如果我理解正确,你想要计算一个项目的最佳销售价值.(或者你试图计算实际值?)
卖家很自然地玩游戏(例如ebay),试图最大化他们的利润.
出于这个原因,我会避免平均/ SD方法:它们对特定销售策略产生的异常值过于敏感.
从博弈论的角度来看,我认为聪明的卖家会通过研究他们的竞争对手及其历史销售额来估计最高的可能销售价格(最大利润):找到最佳点.
出于这个原因,我会记录所有卖家的历史价格直方图,并查看价格的分布,使用接近模式的东西来确定最优价格,即最常见的销售价格.更好的是,我会根据每个卖家的利润(与历史销量成比例)来衡量价格.
我怀疑这会更接近你的最佳市场价值; 如果您正在寻找真正的市场价值,请在下面评论或在我的机器学习公司与我联系
关于@Daniel Collicott帖子中提到的事情的更详细解释:
- >最佳销售价值
- >实际销售价值
- >两者的算法
我试图将一个对象从函数返回到ng-repeat:
<ul ng-controller="Users" ng-repeat="user in users">
<li ng-controller="Albums" ng-repeat="album in getAlbumList(user.name)">
{{album.title}}
</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
.controller('Albums', ['$scope','Imgur', function($scope, Imgur) {
$scope.getAlbumList = function(user) {
Imgur.albumList.get({user:user},
function(value) {
return value.data;
console.log('success');
},
function(error) {
console.log('something went wrong');
}
);
}
}]);
.factory('Imgur', function($resource, $http) {
return {
albumList : $resource('https://api.imgur.com/3/account/:user/albums'),
album : $resource('https://api.imgur.com/3/account/:user/album/:id')
}
});
Run Code Online (Sandbox Code Playgroud)
然而,这种方法会在每次加载页面时崩溃我的浏览器.
这样做的正确方法是什么?
假设我的Firebase集合如下所示:
{
"max":5
"things":{}
}
Run Code Online (Sandbox Code Playgroud)
我如何使用max我的安全规则中的值来限制数量things?
{
"rules": {
"things": {
".validate": "newData.val().length <= max"
}
}
}
Run Code Online (Sandbox Code Playgroud)