I am trying to get the country name from the JSON outside of the JSON scope but somehow it does not seem to pass, what am I doing wrong? I looked into the $rootScope but did not seem to get far with that. It's simple in principle, I would like to use the Country name as a variable in other scopes or on the page itself, also would like to use it in the Controller.cs (.net) to send it to the database
我试图从JSON范围之外的JSON获取国家名称但不知何故它似乎没有通过,我做错了什么?我查看了$ rootScope,但似乎没有那么远。它原则上很简单,我想将Country Name用作其他作用域中的变量或页面本身,也想在Controller.cs(.net)中使用它来将它发送到数据库
app.controller('PageController',
function ($scope, $http) {
var analyticsCountry = "default";
$.getJSON('//www.geoplugin.net/json.gp?jsoncallback=?',
function (data) {
$scope.testing = data;
$scope.testing.country = data.geoplugin_countryName;
//console.log($scope.testing.country);
analyticsCountry = $scope.testing.country;
});
console.log(analyticsCountry);
$scope.GetTrendingCDsByCountry = function () {
$http({
method: 'Get',
url: "/CD/GetTrending?id=" + analyticsCountry
})
.success(function (data, status, headers, config) {
$scope.cds= data;
})
.error(function (data, status, headers, config) {
$scope.message = 'Unexpected Error';
});
};
});
1 个解决方案
#1
0
If you want it in the view. just bind it into $scope
and you would be good to go. and as @Suren Srapyan said the callback would execute after the console.log()
that has value "default"
saved.
如果你想在视图中。只需将它绑定到$ scope即可,你会很高兴。并且正如@Suren Srapyan所说,回调将在具有值“default”的console.log()之后执行。
app.controller('PageController',
function ($scope, $http) {
$scope.analyticsCountry = "default";
$scope.GetTrendingCDsByCountry = function () {
$http({
method: 'Get',
url: "/CD/GetTrending?id=" + $scope.analyticsCountry
})
.success(function (data, status, headers, config) {
$scope.cds= data;
})
.error(function (data, status, headers, config) {
$scope.message = 'Unexpected Error';
});
};
$.getJSON('//www.geoplugin.net/json.gp?jsoncallback=?',
function (data) {
$scope.testing = data;
$scope.testing.country = data.geoplugin_countryName;
//console.log($scope.testing.country);
$scope.analyticsCountry = $scope.testing.country;
console.log($scope.analyticsCountry);
$scope.GetTrendingCDsByCountry();
});
});
Now it will work and would be easily accessed in the view as well.
现在它可以工作,也可以在视图中轻松访问。
#1
0
If you want it in the view. just bind it into $scope
and you would be good to go. and as @Suren Srapyan said the callback would execute after the console.log()
that has value "default"
saved.
如果你想在视图中。只需将它绑定到$ scope即可,你会很高兴。并且正如@Suren Srapyan所说,回调将在具有值“default”的console.log()之后执行。
app.controller('PageController',
function ($scope, $http) {
$scope.analyticsCountry = "default";
$scope.GetTrendingCDsByCountry = function () {
$http({
method: 'Get',
url: "/CD/GetTrending?id=" + $scope.analyticsCountry
})
.success(function (data, status, headers, config) {
$scope.cds= data;
})
.error(function (data, status, headers, config) {
$scope.message = 'Unexpected Error';
});
};
$.getJSON('//www.geoplugin.net/json.gp?jsoncallback=?',
function (data) {
$scope.testing = data;
$scope.testing.country = data.geoplugin_countryName;
//console.log($scope.testing.country);
$scope.analyticsCountry = $scope.testing.country;
console.log($scope.analyticsCountry);
$scope.GetTrendingCDsByCountry();
});
});
Now it will work and would be easily accessed in the view as well.
现在它可以工作,也可以在视图中轻松访问。