I'm developing an app with the Ionic framework based on angularjs. I'd like to let generate HTML elements or components from a JSON file. These are buttons, lists, labels, etc.
My JSON objects look like this:
[
{
"category": "communicationPage",
"type": "Button",
"id": "communicationButton",
"icon": "ion-chatboxes",
"name": "Communication",
"onClick": "window.location.href='communicationPage.html'",
"ngclick": "open()",
"ngcontroller": "openctrl",
"color": "white",
"background-color": "#ff5db1",
"font-size": "20px"
},
{
"category": "servicePage",
"type": "Button",
"id": "serviceButton",
"icon": "ion-briefcase",
"name": "Service",
"onClick": "window.location.href='servicePage.html'",
"color": "blue",
"background-color": "#009900",
"font-size": "26px"
}
]
I can access via my Controller on the JSON file and parse as follows:
myApp.controller('generateHTMLCtrl', function ($scope, $http) {
$http.get('myJSONFile.json').success(function(data){
$scope.components = data;
//...
});
});
The code translates of course nothing.
- My question is, how can I adapt my JavaScript code so that from a
JSON object following HTML element is generated?:
<button style="color: blue; background-color: #ff5db1; font-size: 20px" onclick="window.location.href='communicationPage.html'" id="communicationButton" class="button">
<i class="ion-chatboxes"></i> <br> Communication
</button>
Once my JSON object is located always in the JSON file, should always be created the HTML element on the page.
- The second question is how I can position this generated HTML element just in my HTML page?
I want that the HTML element is generated between the responsive grid element, such as:
<div class="row responsive-sm">
<div class="col">
<!-- The Button should be generated hier-->
</div>
</div>
- The third and final question is how I can let generate the HTML
element on the appropriate page? Such as: If in JSON object the key-value pair of
"category": "communicationPage"
occurs should the corresponding HTML element be created on'communicationPage.html'
I would look forward to an example. Many thanks.
<button>
for redirect and not use an<a>
tag with an href? Question #3 is very unclear what you are asking – charlietfl Aug 4 '15 at 12:59<a></a>
and what worse by using<button></button>
? – Ramosta Aug 4 '15 at 13:04"category": "communicationPage"
is in my JSON object located this button automatically should be generated on the HTML page namedcommunicationPage.html
. I want to work only in my JSON file not with the HTML-Code in html.page. So I would like to add the HTML elements on my page or delete from my page by editing the JSON-File. So the components should be deleted or added on the targeted page. – Ramosta Aug 4 '15 at 13:19