Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用AngularJS承诺来确保获取的数据更新范围?_Angularjs_Promise - Fatal编程技术网

如何使用AngularJS承诺来确保获取的数据更新范围?

如何使用AngularJS承诺来确保获取的数据更新范围?,angularjs,promise,Angularjs,Promise,我正在将一些JSON请求从控制器中分离出来,然后放入angular中的工厂服务中,并让它工作起来。目前,当我更新数据时(就像请求新数据一样),我必须发出两次请求才能真正看到更新的数据。从我目前对Angular有限的知识来看,我认为我正在失去承诺方面。然而,我真的不确定如何最好地继续下去。代码如下: (函数(角度){ "严格使用",; var myApp=angular.module('weatherApp',[]) .controller('MyWeatherController',['$sc

我正在将一些JSON请求从控制器中分离出来,然后放入angular中的工厂服务中,并让它工作起来。目前,当我更新数据时(就像请求新数据一样),我必须发出两次请求才能真正看到更新的数据。从我目前对Angular有限的知识来看,我认为我正在失去承诺方面。然而,我真的不确定如何最好地继续下去。代码如下:

(函数(角度){
"严格使用",;
var myApp=angular.module('weatherApp',[])
.controller('MyWeatherController',['$scope','$http','$log','weather',函数($scope,$http,$log,weather){
$scope.city='辛辛那提';
$scope.units=‘英制’;
$scope.updateData=函数(){
updateData($scope,$http,$log);
};
//获取天气数据的初始运行
$scope.updateData();
}])
//设置服务工厂以获取当前和预测数据
.factory('weather',['$http',function($http){
var服务={};
services.updateData=函数(作用域、http、日志){
this.updateCurrent(作用域、http、日志);
}
//获取当前数据
services.updateCurrent=函数(作用域、http、日志){
//从api获取当前天气数据
var url='1〕http://api.openweathermap.org/data/2.5/weather';
http.jsonp(url,{params:{
问:scope.city,
单位:scope.units,
回调:“JSON_回调”
}})
.success(函数(数据、状态、标题、配置){
scope.main=data.main;
scope.wind=data.wind;
scope.description=data.weather[0]。description;
})
.error(函数(数据、状态、标题、配置){
log.error('无法从'+url'检索数据);
});
};
返回服务;
}]);
}(窗口角度)
正文{
宽度:50%;
利润率:2米自动;
}    
氢{
背景:绿色;
颜色:白色;
填充:10px;
页边距底部:0;
}
输入{
边缘底部:1米;
}
.科{
边缘底部:2米;
背景:#f0;
填充:10px;
}

天气检查器
{{城市}的当前天气
城市、州/邮政编码

单位 米制的 帝国的
更新 {{data.name} {{description}}

温度:{{main.temp}}

风速:{{Wind.speed}

(函数(角度){
"严格使用",;
var myApp=angular.module('weatherApp',[])
.controller('MyWeatherController',['$scope','$http','$log','weather',函数($scope,$http,$log,weather){
$scope.city='辛辛那提';
$scope.units=‘英制’;
$scope.updateData=函数(){
updateData($scope,$http,$log);
};
//获取天气数据的初始运行
$scope.updateData();
}])
//设置服务工厂以获取当前和预测数据
.factory('weather',['$http',function($http){
var服务={};
services.updateData=函数(作用域、http、日志){
this.updateCurrent(作用域、http、日志);
}
//获取当前数据
services.updateCurrent=函数(作用域、http、日志){
//从api获取当前天气数据
var url='1〕http://api.openweathermap.org/data/2.5/weather';
http.jsonp(url,{params:{
问:scope.city,
单位:scope.units,
回调:“JSON_回调”
}})
.success(函数(数据、状态、标题、配置){
scope.main=data.main;
scope.wind=data.wind;
scope.description=data.weather[0]。description;
$scope.digest();
})
.error(函数(数据、状态、标题、配置){
log.error('无法从'+url'检索数据);
});
};
返回服务;
}]);
}(窗口角度)
正文{
宽度:50%;
利润率:2米自动;
}    
氢{
背景:绿色;
颜色:白色;
填充:10px;
页边距底部:0;
}
输入{
边缘底部:1米;
}
.科{
边缘底部:2米;
背景:#f0;
填充:10px;
}

天气检查器
{{城市}的当前天气
城市、州/邮政编码

单位 米制的 帝国的
更新 {{data.name} {{description}}

温度:{{main.temp}}

风速:{{Wind.speed}

你有两种方法(可能更多)可以到这里。 您可以创建一个promise并返回promise中http调用返回的数据,这些数据将在控制器中解析。查看$q文档中的第二个代码块,了解如何执行此操作

但是,我建议您在服务中创建一个公共属性来保存数据。然后,您可以从控制器访问它。由于Angular的数据绑定,当服务器响应最终返回并且数据存储在服务中时,控制器将自动“查看”更新的值

(函数(角度){
"严格使用",;
var myApp=angular.module('weatherApp',[])
.controller('MyWeatherController',['$scope','$http','$log','weather',函数($scope,$http,$log,weather){
//在这里,您可以将服务公开到您的作用域中
//使用weather.result获取视图中的所有数据
$scope.weather=天气;
$scope.city='辛辛那提';
$scope.units=‘英制’;
$scope.updateData=函数(){
天气预报(
(function (angular) {
    'use strict';

    var myApp = angular.module('weatherApp', [])

        .controller('MyWeatherController', ['$scope', '$http', '$log', 'weather', function ($scope, $http, $log, weather) {
        $scope.city = 'Cincinnati';
        $scope.units = 'imperial';

        $scope.updateData = function () {
            weather.updateData($scope.city, $scope.units).
            then(function (res) {
                var data = res.data;
                $scope.main = data.main;
                $scope.wind = data.wind;
                $scope.description = data.weather[0].description;
            },function (data) {
                $log.error('Could not retrieve data from ' + url);
            })

        };

        // Initial run to fetch weather data
        $scope.updateData();
    }])


    // Set up a service factory to fetch current and forecast data
    .factory('weather', ['$http', '$log', function ($http, $log) {
        var services = {};

        services.updateData = function (city, units) {
            return this.updateCurrent(city, units);
        };

        // Get current data
        services.updateCurrent = function (city, units) {
            // Fetch current weather data from api
            var url = 'http://api.openweathermap.org/data/2.5/weather';

            return $http.jsonp(url, {
                params: {
                    q: city,
                    units: units,
                    callback: 'JSON_CALLBACK'
                }
            }).success(function (data, status, headers, config) {
                return data;
            })
            .error(function (data, status, headers, config) {
                $log.error('Could not retrieve data from ' + url);
                return data;
            });

        };

        return services;
    }]);

}(window.angular));