Search code examples
angularjsmodel-view-controllerangularjs-scopesingleton

AngularJS: create single controller but use it in different parts of the page with different data


I need to write piece of code with AngularJS which will build some table with logic. The information for the table should be obtained from the server. Under the table on the page there should be a button, which will create one more table, but (possibly) with different data from the server, and so on. So, I should use the same controller (because all logic remains the same in every next table), but data, some supporting variables and objects should be different for EVERY table. Previously loaded tables should also remain on the page and support all user functions binded to them. How I can do this? As far as I know, services are singleton objects, so this way is not acceptable.


Solution

  • This is actually what directives are for.

    If that seems too complicated, ng-repeat will create a scope for each repeated item that you can then use in ng-controller.