Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm using Angular JS to work on a proof of concept for a SPA that needs to be able to handle dynamic form generation. This project is essentially an interview style application that will have a user go through multiple screens to answer questions to be rated against the server. I'm trying to write a generic controller that can handle any interview page, which gets the actual page content from a server call (the view is obviously going to be page specific). I can handle the page content fairly easily by just wrapping my returned data in a containing variable so that my view just has to access that root variable and path down using JSON

Example (pageData being the generic container variable):

ng-model="pageData['AccountInput.Address1'].value"

The problem arises when I want to integrate something a little more involved than just simple data output - something like the ngTable directive to implement pagination or sorting to a standard table. The logic for this is supposed to be contained within the controller. So my question is whether it's possible to make my controller generic enough that I can look at my JSON response and create one or more pieces of dynamic code in my controller to create something along the lines of this ngTable? Basically, check for any arrays in my response that has a properties object that can be used as the settings for the table and have the controller iterate through all of the JSON to find these.

For reference, here's the code required for the ngTable in the controller as per the links above.

$scope.tableParams = new ngTableParams({
    page: 1,            // show first page
    total: data.length, // length of data
    count: 10           // count per page
});
share|improve this question
2  
What you want to achieve is not supposed to be handled by a controller, but by a directive. What I'd do is write a generic directive that, given any question element, is able to output the appropriate element, be it an input, a textarea, or a ngTable. –  frapontillo Oct 9 '13 at 18:52
    
To add on to the comment by @frapontillo this answer might help. stackoverflow.com/questions/16340236/… –  lucuma Oct 9 '13 at 21:46
    
This is where I get confused...ngTable is a directive itself. So I understand that utilizing the ngTable directive as part of my overall <table> HTML, but the configuration and parameters of said table escape me as being able to do in a directive. Is there any way you could write up a simple example (doesn't have to utilize ngTable)? –  Brandon Oct 10 '13 at 1:01
    
Based on the answer @lucuma gave you, you can simply choose to '$compile' a certain element string or another one, depending on the value of the model you bind the directive to. –  frapontillo Oct 10 '13 at 8:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.