I am creating a controller in Angular for which I get the url parts and some parameters with $statePrams
.
I call the $http
service to get data back from a json file. After getting the data back, I assign the specified object element's content -which specification is generated from $stateParams
- to a $scope
variable which I can process in my view to generate some kind of unordered list elements.
My problem is when I generate for example the path as result['data']['datas']['timeline']
, then it won't work, and give me ngRepeat:dupe
error.
However there are no duplicates at all. If I just type it in manually like $scope.naviData = result['data']['datas']['timeline'];
, it will generate the object, give me all the needed data and draw the li
elements in the view.
How could I solve this problem since I do not know how to do this dynamic object element access otherwise. Here are two images:
First one is with manual writing
Second one is with dynamically generated variable
The first
console.info
in both case are the state params I generate the path from. Also here are the codes doing the magic part for me.
angular.module('MPWeb.datas', [])
.controller('DataDetailsCtrl', function($scope, $state, $stateParams, $http) {
$scope.params = $stateParams;
// set datas for dynamic object handling
var base = 'data.datas';
var category = ($scope.params.categoryId) ? $scope.params.categoryId : '';
var article = ($scope.params.articleId) ? $scope.params.articleId : '';
var stateConfig = (article) ? {
params: {
prefix: base,
categoryId: category,
articleId: article
}
} : {
params: {
prefix: base,
categoryId: category
}
};
console.info(stateConfig.params); // this is the first console entry on the screenshots
// with this function I get back a standard joined string
var objToString = function(obj) {
var tabjson = [];
for (var p in obj) {
if (obj.hasOwnProperty(p)) {
tabjson.push(obj[p]);
}
}
tabjson.push()
return tabjson.join('.');
};
// generate sideNavigation
$http({
method: 'GET',
url: './json/mp-navigation.json'
}).then(function successCallback(result, status, headers) {
var temp = objToString(stateConfig.params);
var naviTemp = (stateConfig.params.articleId) ? temp.replace(/\.[^.]+$/, "") : temp;
naviTemp = "result['" + naviTemp + "']";
naviTemp = naviTemp.replace(/\./g, "']['"); // dynamically generated path with bracket notation method
//$scope.naviData = naviTemp; // if I use this, it gives me the error
$scope.naviData = result['data']['datas']['timeline'];
console.info(naviTemp);
}, function errorCallback(result, status, headers) {
console.warn(status);
});
});
Also if I use track by $index
in my view it will just generate li
elements from the letters of my generated path.