user4815703
user4815703

Reputation:

Controllers and how they should be implemented in Angular?

Sorry if this seems like a stupid or simple question but I am a little confused, I have been looking up many different kinds of tutorials for Angular to understand the concept and how to create an application.

The issue is how to you attach a Controller to the Page, I have seen two methods:

  1. Add the controller script to the page
  2. Display Controller inside the app.js where the Website Routing is.

Here is what I have at the moment please let me know if there is any issues in this code:

    var app = angular.module('myApp', [
    'ngRoute'
]);

app.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/', {
        templateUrl: 'partials/home.html',
        controller: 'homeController'
      }).
      when('/login', {
        templateUrl: 'partials/login.html',
        controller: ''
      }).
      when('/signup', {
        templateUrl: 'partials/signup.html',
        controller: ''
      }).
      when('/dashboard', {
        templateUrl: 'partials/dashboard.html',
        controller: ''
      }).
      otherwise({
        redirectTo: '/404',
        templateUrl: 'partials/404.html' 
      });
  }]);

app.controller('homeController', ['$scope', function($scope) {
    $scope.message = "This is the Home Page";
}]);

Again I am really new to Angular.

Updated to single Controller file: app.js:

    var app = angular.module('myApp', [
    'ngRoute'
]);

app.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/', {
        templateUrl: 'partials/home.html',
        controller: 'controllers/homeController.js'
      }).
      when('/login', {
        templateUrl: 'partials/login.html',
        controller: ''
      }).
      when('/signup', {
        templateUrl: 'partials/signup.html',
        controller: ''
      }).
      when('/dashboard', {
        templateUrl: 'partials/dashboard.html',
        controller: ''
      }).
      otherwise({
        redirectTo: '/404',
        templateUrl: 'partials/404.html' 
      });
  }]);

controller file:

app.controller('homeController', ['$scope', function($scope) {
    $scope.message = "This is the Home Page";
}]);

Upvotes: 0

Views: 100

Answers (4)

Abdul23
Abdul23

Reputation: 6005

Change your route specification to the following code:

app.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
  when('/', {
    templateUrl: 'partials/home.html',
    controller: 'homeController' //change here
//controller should be the name of the controller,
//not the file containing the controller function
  }).
  when('/login', {
    templateUrl: 'partials/login.html',
    controller: ''
  }).
  when('/signup', {
    templateUrl: 'partials/signup.html',
    controller: ''
  }).
  when('/dashboard', {
    templateUrl: 'partials/dashboard.html',
    controller: ''
  }).
  otherwise({
    redirectTo: '/404',
    templateUrl: 'partials/404.html' 
  });
}]);

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691695

That's a purely organizational choice. As long as the browser has the code of the controller available, it doesn't matter.

But unless you're creating a tiny demo, having all the controllers defined in a single JavaScript file will quickly become unmanageable: the file will be too large, you'll search for the controllers constantly, and everyone in the team will modify the same file, leading to conflicts, etc.

The simple rule is: one JS file per AngularJS component.

If you're concerned about two many JS files having to be loaded by the HTML page in production, then make sure to learn using gulp or grunt, and to generate a single minified JS file from all the small JS files used during development.

EDIT:

the controller attribute of the route is not supposed to be the path of a JS file. It's supposed to be the name of a controller. It should thus stay exactly as it was in the first, working example.

You need to understand how the browser works: if the HTML contains two <script> elements, it works the same way as if it had a single one with the code of the two scripts concatenated. So splitting the code in two files doesn't change the way the code is written.

Upvotes: 0

Jean Cedron
Jean Cedron

Reputation: 732

I create a Controller for every model in my database: e.g: ProjectController.js, PeopleController.js, etc. And I use app.js just for routing and general controllers like header, footer, etc.

There isn't a strict way to do it, you have to decide it based on your architecture design. But i can give you a tip: Never define your controllers in your .html file because it makes it awful and less readable.

Upvotes: 0

Jeremy Cal
Jeremy Cal

Reputation: 443

Nope, your code is fine. I generally use two different files app.js for all the routing options and a controller.js file for the different controllers. A single file seems a bit too cluttered to me. A single file per controller works but I see for most usercases it turns out just a few lines of code per page for me, but you can if you have extensive codes in each controller

Upvotes: 0

Related Questions