Creating a dynamic tree using JSON is a common task in AngularJS. To achieve this, you can store your JSON data in a separate file and then read it externally. Here's an example of how you can do this:
Sample Controller Code:
$scope.myjson = {
"option1": [
{
"child":[{"label":"Test1" },{"label":"Test2"}],
"id": "option1"
}
],
"option2": [
{
"child":[{"label":"Test1.1",}],
"id": "option2"
}
],
// Add more options as needed...
}
Reading JSON Array (In Controller):
angular.forEach($scope.myjson, function(value, key)
{
if (key === 'option1')
{
for(var t=0; t < $scope.myjson[key][0].child.length; t++)
{
// Perform operations
}
}
});
If you want to call a JSON file externally to create the tree, you can make use of AngularJS's built-in functions. By using services like $http or $resource, you can easily fetch the JSON file and incorporate it into your tree creation logic.