How do I add a controller to a module in Angular JS

angularjs

Solution

Based on what you have shared, the the controller should be created on admin module such as

var adminModule=angular.module('admin');   // This syntax get the module
adminModule.controller('AdminContentController',
    ['$scope', 'entityService', 'gridService', 'gridSelectService', 'localStorageService',
    function ($scope, entityService, gridService, gridSelectService, localStorageService) {
        $scope.entityType = 'Content';

You could also define the controller in continuation of your admin module declaration.

Problem

I have the following in my app.js: ``` var app = angular.module('app', ['admin', 'ui.compat', 'ngResource', 'LocalStorageModule']); app.config(['$stateProvider', '$locationProvider', function ($stateProvider, $locationProvider) { $locationProvider.html5Mode(true); var home = { name: 'home', url: '/home', views: { 'nav-sub': { templateUrl: '/Content/app/home/partials/nav-sub.html', } } }; $stateProvider.state(home) }]) .run(['$rootScope', '$state', '$stateParams', function ($rootScope, $state, $stateParams) { $rootScope.$state = $state; $rootScope.$stateParams = $stateParams; $state.transitionTo('home'); }]); ``` in admin.js: ``` angular .module('admin', ['ui.state']) .config(['$stateProvider', '$locationProvider', function ($stateProvider, $locationProvider) { $locationProvider.html5Mode(true); var admin = { name: 'admin', url: '/admin', views: { 'nav-sub': { templateUrl: '/Content/app/admin/partials/nav-sub.html', } } }; var adminContent = { name: 'admin.content', parent: admin, url: '/content', views: { 'grid@': { templateUrl: '/Content/app/admin/partials/content.html', controller: 'AdminContentController' } } } $stateProvider.state(admin).state(adminContent) }]) ``` I am confused about how to wire up my AdminContentController. Currently I have the following: ``` app.controller('AdminContentController', ['$scope', 'entityService', 'gridService', 'gridSelectService', 'localStorageService', function ($scope, entityService, gridService, gridSelectService, localStorageService) { $scope.entityType = 'Content'; ``` Can someone verify if this is the correct way for me to set up my module and add it to app. Should I be adding the controller to the app: ``` app.controller('AdminContentController', ``` or should this belong to the module 'admin'. If it should then how should I wire it up?

Original source