javascript - Use one controller for routing in Angular JS -


here simple example of creating routes angular js:

var empapp = angular.module('empapp', [     'ngroute',     'empcontrollers' ]);  empapp.config(['$routeprovider', function ($routeprovider) {      $routeprovider.when('/list', {         templateurl: 'employee/list.html',         controller: 'listcontroller'     }).     when('/create', {         templateurl: 'employee/edit.html',         controller: 'editcontroller'     }).     when('/edit/:id', {         templateurl: 'employee/edit.html',         controller: 'editcontroller'     }).     when('/delete/:id', {         templateurl: 'employee/delete.html',         controller: 'deletecontroller'     }).     otherwise({         redirectto: '/list'     });  }]); 

source here

as can see there different controllers every action (add, delete, etc.)

my question next : possible create 1 controller function has same logic in controllers above (just create controller empctrl , add functions add(), delete() etc.) , apply functions routes? example :

$routeprovider.when('/list', {         templateurl: 'employee/list.html',         controller: 'empctrl' <-- here somehow use empctrl.list()     }).     when('/create', {         templateurl: 'employee/edit.html',         controller: 'empctrl' <-- here somehow use empctrl.add()     }) 

and if possible approach?

yes, it's possible. wouldn't it's approach. controllers meant take information , bind scope - no more. if 2 states have same controller means sharing information, , 1 controller/state instead.


Comments