2017-03-27 1 views
1

dans ma page il y a un bouton et je veux mettre des liens dépendent du pays de l'utilisateur.Comment changer les liens par GeoIP avec angularjs?

J'ai mis ce code en angulaire par exemple pour comprendre mon point.

ce code ne fonctionne pas pour moi.

ai-je raté quelque chose?

<html> 

<head> 

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
<link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet"> 
<link href="http://getbootstrap.com/assets/css/ie10-viewport-bug-workaround.css" rel="stylesheet"> 
<link href="http://getbootstrap.com/examples/jumbotron-narrow/jumbotron-narrow.css" rel="stylesheet"> 

<script src="http://getbootstrap.com/assets/js/ie-emulation-modes-warning.js"></script> 

<script> 
var app = angular.module('gJson', []); 
app.controller('gCtrl', function($scope, $http) { 
    $http.get("//freegeoip.net/json/?callback=myGeoIP") 
    .success(function myGeoIP(d) { 
     if (d.country_code === 'US' || d.country_code === 'CA') { 

     $scope.alertz = "This Offer Available in + d.country_code"; 
     $scope.uslink = 'http://example.US'; 

     } 
     else 
     { 
     $scope.alertz = "This Offer Not Available in + d.country_code"; 
     $scope.uslink = "http://Example.net"; 
     } 
    }); 
}); 

</script> 

</head> 


<body ng-app="gJson"> 

<div class="container" ng-controller="gCtrl"> 
    <div class="jumbotron"> 
     <p>{{alertz}}</p> 
     <a class="btn btn-lg btn-success" ng-href="{{uslink}}" onclick="window.open(this.href, '_self', 'location=yes', 'toolbar=yes');"> 
     Enter 
     </a> 
    </div> 
</div> 

</body> 

</html> 

Répondre

1

modifier l'URL //freegeoip.net/json/?callback=myGeoIP-//freegeoip.net/json/?callback

il retourne maintenant chaîne comme myGeoIp({"city":"","country_code":""...});

Lorsque vous modifiez l'URL le résultat de succès sera un objet et votre code fonctionnera