Javascript 如何在Angular JS中访问特定范围之外的数据?
我正在尝试获取比赛之外的总得分 然后是get请求的(函数()Javascript 如何在Angular JS中访问特定范围之外的数据?,javascript,angularjs,Javascript,Angularjs,我正在尝试获取比赛之外的总得分 然后是get请求的(函数() 首先,我尝试了javascript中的全局变量 现在我试着使用一家工厂,任何帮助都会很感激 您可以使用服务存储该数据: //declaring the module var app = angular.module("sachin", ["ng-fusioncharts"]); //declaring a factory app.factory('team',function(){ return
您可以使用服务存储该数据:
//declaring the module
var app = angular.module("sachin", ["ng-fusioncharts"]);
//declaring a factory
app.factory('team',function(){
return {
runs_aus : ''
};
});
app.controller("myCtrl", function($scope,$http,team){
$scope.australia=[];
$scope.total_runs_aus=0;
//fetching data from JSON
$http.get("convertcsv.json").then(function(response){
$scope.sachin_data=response.data;
angular.forEach($scope.sachin_data, function(value, key){
// manipulating data
if (value.opposition=="v Australia"){
if (value.batting_score=="-"|| value.batting_score == "TDNB" || value.batting_score == "DNB")
$scope.total_runs=$scope.total_runs;
else if (value.batting_score.substr(value.batting_score.length - 1) == "*"){
value.batting_score = value.batting_score.substr(1);
$scope.total_runs_aus+=parseInt(value.batting_score,10)
}
else
$scope.total_runs_aus+=parseInt(value.batting_score,10);
});
$scope.australia.push({ runs:$scope.total_runs_aus});
team.runs_aus=$scope.total_runs_aus;
//got final result in $scope.total_runs_aus
console.log(team.runs_aus);
//printing inside the scope(works fine)
});
console.log(team.runs_aus);
//printing outside the scope(can't access)
您只需要在控制器上注入
MyService
,并像访问MyService.myObject=something
一样访问它。需要了解的重要部分是,您正在进行异步操作。即使数据尚未从$http.get()
返回,代码仍将继续执行并打印控制台日志。您的代码需要考虑到这一点,并在数据解析后运行对其进行操作的代码
.then()需要一个函数作为方法签名的参数。例如:
app.service('MyService', function() {
var self = {
'myString': 1,
'myObject': {},
'myArray': [],
'doSomething': function(param) {
self.myString = param
},
'anotherFunction': function() {
return true;
}
}
return self;
});
或
或者将多个承诺链接在一起(您必须将angular的$q添加为依赖项):
请看一看:
原样代码可能有语法错误,因为它尚未经过测试。链接是一般参考,并非特定于$q,但概念是一致的。您使用的是异步数据。将使用totalRuns变量的逻辑也移到then()函数中。控制台日志在解决方案之前被执行,因此数据不可用。如果希望在“代码”> $http.GET请求被解析之后执行特定代码,请考虑使用承诺链。看一个例子:你明白我的建议吗?例如,将控制台日志移动到.then()函数中if else块的下面。then()中的代码将在
$http.get()执行完毕后执行。给定控制台日志的位置,这些日志会在数据可用之前执行。可以使用承诺链接吗?我正在添加另一个.then(console.log(“test”);在第一个http.get(“url”)。然后(#code.)。然后(console.log(“test”));但对meThis不起作用因为它与异步操作无关,所以它不会起作用。
$http.get("convertcsv.json").then(function(response){
$scope.sachin_data=response.data;
}).then(function() {
console.log('Value in scope:', $scope.sachin_data);
});
function processData = function() {
console.log('Value in scope:', $scope.sachin_data);
};
$http.get("convertcsv.json").then(function(response){
$scope.sachin_data=response.data;
}).then(processData);
function processData1 = function(data) {
//Create a deferred object.
var defer = $q.defer();
//Do something with data.
console.log('Value in scope:', data);
//Pass data to next promise in promise chain.
defer.resolve(data);
//Resolve data to be returned.
return defer.promise;
};
function processData2 = function(data) {
//Create a deferred object.
var defer = $q.defer();
//Do something else with data.
console.log('Value in scope:', data);
//Pass data to next promise in promise chain.
defer.resolve(data);
//Resolve data to be returned.
return defer.promise;
};
$http.get("convertcsv.json")
.then(processData1)
.then(processData2);