I parse JSON objects to create html elements. In my case, I create from json file Buttons:
{
"type": "button",
"id": "comButton",
"icon": "ion-chatboxes",
"name": "Communication",
"onclick": "",
"controller": "somemthctrl",
"ngclick": "launchSomemethod()",
"color": "white",
"backgroundcolor": "#ff5db1",
"font-size": "20px"
}
Controller:
myApp.controller('generateButtonCtrl', function ($scope, $http) {
$http.get('JSON/buttons.json').success(function(data){
$scope.components = data;
});
});
From the HTML page, I call the components from the json file:
<a ng-repeat="component in components"
style="color:{{component.color}}; background-color:{{component.backgroundcolor}} "
id="{{component.id}}"
class="{{component.type}}"
href="{{component.onclick}}"
ng-click="{{component.ngclick}}"
ng-controller="{{component.controller}}">
<i class="{{component.icon}}"><br></i>
{{component.name}}
</a>
In the case ng-click="{{component.ngclick}}"
und ng-controller="{{component.controller}}"
will not be included.
At the appropriate places I get from my editor WebStorm following error: Identifier or String literal or numeric literal expected.
I have a {{expression}}
Problem. How can I integrate the ng-controller
and ng-click
as a string from a json object?
This is quite tricky. Angular team on their doc suggests that controller should be registered to a module while the DOM is being parsed.
All the $scope properties will be available to the template at the point in the DOM where the Controller is registered.
Link: Angular controllers
I'm not sure that it's possible the ng-controller could be a {{ expression }}
.
The workarround it's creating a function that returns the name of the controller that you want and you can assign to a var inside the controller...
In other case, why you want to use a different controller for each button?