Reputation: 189
I wanted to know how to load 2 diffrent json based on condition
.controller("myCtrl", ['$scope','$http', function($scope, $http) {
if (lang==1){
$scope.myVariable = [];
$http.get('json/data.json').success (function(data){
$scope.myVariable = data;
});
}
else if (lang==2){
$scope.myVariable = [];
$http.get('json/data_ar.json').success (function(data){
$scope.myVariable = data;
});
}
}])
the variable passes to lang but json not loading if lang==2
I want to load data.json if english and data_ar.json for arabic. I do this on click of anchor tag
Upvotes: 0
Views: 207
Reputation: 23211
please put your following code just inside any method like this:
.controller("myCtrl", ['$scope','$http','$rootScope','$route', function($scope, $http,$rootScope,$route) {
if ($rootScope.lang==1){
$scope.myVariable = [];
$http.get('json/data.json').success (function(data){
$scope.myVariable = data;
});
}
else if ($rootScope.lang==2){
$scope.myVariable = [];
$http.get('json/data_ar.json').success (function(data){
$scope.myVariable = data;
});
}
$scope.loadData=function(lang){
$rootScope.lang=lang;
$route.reload();
}
}])
and html code:
<a ng-click="loadData(2)">arabic formate</a>
or you have done like this then please post full html and controller code related to that functionality so that i can see, where you have done mistake.
Upvotes: 1
Reputation: 6024
You defined another lang
variables in lang
switching script. So change:
if(lang==1){ var lang=2; } else if(lang==2){ var lang=1; }
to:
if(lang==1){ lang=2; } else if(lang==2){ lang=1; }
Upvotes: 1
Reputation: 149
I guess, you have defined lang
in the HTML page. Make sure, that the definition is wrapped with an element with ng-controller="myCtrl"
attribute.
Now use if ($scope.lang==2)
instead of if (lang==2)
Upvotes: 1