Ramanaa Gj Ramanaa Gj - 1 month ago 8
AngularJS Question

way to provide a path to controller file using ui-router

is there any way to provide a path to a controller file by using UI-ROUTER

this is my code

var kraveln = angular.module('kraveln', ['ui.router' , 'ngMessages' , 'ngDialog' ]);

kraveln.config(function($stateProvider, $urlRouterProvider) {

$urlRouterProvider.otherwise('/home');

$stateProvider
// for home_page
.state('home', {
url: '/home',// if home page means
views: {
'':
{
templateUrl: 'app/views/home_search_page.html',
controller: 'app/views/sample_controller.js' // sample controller for loading file.
},
'header_part':
{
templateUrl: 'app/views/header_one.html' //load first header
},
'footer_part':
{
templateUrl: 'app/views/footer.html' //load second footer
}
}
})


as like the template url can we load controller URL i tried but can't able to do so

Answer

Yes you can give a specific controller to every view in a state.

.state('home',{
          url: '/home',
          views: {
            '': {
              templateUrl: 'app/main/main.html',
              controller: 'MainController',
              controllerAs: 'main'
            },
            'header': {
              templateUrl: 'app/main/header.html',
              controller: 'HeaderController',
              controllerAs: 'header'
            },
            'footer': {
              templateUrl: 'app/main/footer.html'
            }
          }
   })

reference link