Update $scope var from http.get
Update $scope var from http.get
有人可以解释为什么运行 for 循环后$scope.shopLocation 不会更改为 = special。我需要在控制器中进一步访问此 var 数据,但我无法在 http.get 中访问它
如果有人有任何指向范围/变量等良好解释的链接,我将不胜感激。
.controller('MapCtrl', function($scope, $http) {
$scope.shopLocation = [];
$http.get('/x/xx/xxx/advertsearch_json.php')
.then(function(res) {
$scope.jobs1 = res.data;
var special = [];
var categories = [];
for ( var i=0; i < $scope.jobs1.length; i++ )
categories[$scope.jobs1[i]['CHCO_LOCATION']] = $scope.jobs1[i];
$scope.jobs1 = new Array();
for ( var key in categories )
special.push(categories[key]);
$scope.shopLocation = special;
});
})
我认为
问题是您没有确保其余的逻辑仅在异步操作完成后发生。您需要做的是在http
调用的then
函数中return
所需的信息,然后使用对 promise 的引用来获取该返回值。它的工作原理是这样的:
var myPromise = $http.get('whatever').then(function(resp) {
// do what you need to do
return something; // return the value you'll need later
});
// elsewhere
$scope.foo = function() {
myPromise.then(function(something) {
// use the value you returned ^. the logic here will run when the value is definitely available.
});
};
这样,如果您调用 $scope.foo
,您的逻辑肯定会具有可用的异步操作的值。将其与以下代码进行对比:
$scope.something;
$http.get('whatever').then(function(resp) {
$scope.something = //whatever
});
$scope.foo = function() {
console.log($scope.something); // no guarantee this will be set
};
我相信
您的异步代码导致更新发生在摘要周期之外。因此,该值正在更改,但您不会在屏幕上看到它。
在这种情况下,您需要应用范围 - 因此在$scope.shopLocation = special;
之后添加:
$scope.$apply();
我认为这与特殊范围有关。 在http.get完成后,它被销毁了。 您实际上并没有复制您提供的代码中的内容。 你应该做一个深拷贝。 https://docs.angularjs.org/api/ng/function/angular.copy
.controller('MapCtrl', function($scope, $http) {
$scope.shopLocation = [];
$http.get('/x/xx/xxx/advertsearch_json.php')
.then(function(res) {
$scope.jobs1 = res.data;
var special = [];
var categories = [];
for ( var i=0; i < $scope.jobs1.length; i++ )
categories[$scope.jobs1[i]['CHCO_LOCATION']] = $scope.jobs1[i];
$scope.jobs1 = new Array();
for ( var key in categories )
special.push(categories[key]);
$scope.shopLocation = angular.copy(special);
});
})
所以这就像调用后面的函数一样简单,该函数使用"then"中的新数据
$http.get('/pb3/corporate/Apostrophe/advertsearch_json.php')
.then(function(res) {
$scope.jobs1 = res.data;
var special = [];
var categories = [];
for ( var i=0; i < $scope.jobs1.length; i++ )
categories[$scope.jobs1[i]['CHCO_LOCATION']] = $scope.jobs1[i];
$scope.jobs1 = new Array();
for ( var key in categories )
special.push(categories[key]);
$scope.shopLocation = special;
**createPoints();**
});
感谢大家的帮助
相关文章:
- 如何将PHP get查询转换为Meteor's HTTP.get()
- Angularjs http.get() Cross-Origin 请求被阻止
- Angular js http get返回所有页面
- Angular.js-$http.get-使用文件中的缓存
- 当我在chrome中切换设备模式时,$http.get请求失败
- Angular http.get没有'我不喜欢在开头和结尾加方括号的文本文件
- 从javaspring方法管理http.get响应
- 我可以在React应用程序中使用什么作为Angular的等价物;s$http.get
- 如何让foreach循环等待$http.get完成后再继续
- Angular Cross Origin Request CORS失败,但节点http.get()成功返回
- 使用$http.get()-OAuth身份验证下载带有Angular的远程文件
- $http GET URL更改并查找错误的资源
- angularJS $http.get communicating with API
- 在AngularJS中使用$http.get检索JSON时出现错误400
- AngularJS $http GET 请求到本地 JSON 文件返回语法错误:意外令牌]
- 使用角度$http.get从服务器下载文件
- AngularJS或JQuery解析错误格式的JSON(http get)
- AngularJS HTTP Get 没有得到响应
- 无法在角度 js 中将参数传递给 http get 请求
- Meteor:使用HTTP.get从客户端访问网站(CORS错误)