0

I am using ng-table directive to show tables with data.

My current view utilising the table is :

<table ng-table="table_params" show-filter="true" ng-controller="table">
...
</table>

I have couple of more pages that need to display the same table (only different data). So the logical thing for me seems to be using one view but different controllers to fill the table with different data. I am not sure whats the Angular way of doing that and how to do it.

I am using angular-route to do the routing with ng-view directive

Michael
  • 22,196
  • 33
  • 132
  • 187

2 Answers2

1

If you are using ngRoute then set up the controller in the $routeProvider config, not in the template.

$routeProvider.when('/table/1', {
    templateUrl: 'common_table.html',
    controller: 'Table1Controller'
});

doc: ngRoute.$routeProvider#methods_when

Oliver
  • 4,471
  • 2
  • 21
  • 18
1

In your case, don't put ng-controller in the html, but in js (i.e. app.js)

.when('/table', {
  templateUrl: 'views/table.html',
  controller: 'tableCtrl'
})
.when('/yetanothertable', {
  templateUrl: 'views/table.html',
  controller: 'yetanothertableCtrl'
})

If you want to have a separate section with another controller, put it in a separate html and use ng-include. In your views/table.html:

<div>Main content here...</div>
<div ng-include="'views/extracontent.html'"></div>

Then put in your 'views/extracontent.html' a ng-controller. Remember that ng-include accepts var as well so you can declare it in the tableCtrl and others to include different views.

Tino
  • 436
  • 4
  • 6
  • in case of ng-router you are right, my problem with defining controllers in ng-router is that sometimes I want to have multiple controllers in single page, not specifying the controller is easier since angular defers which controller to use by looking at the template. – Michael Dec 16 '13 at 21:16