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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.