angularjs - Controllers and how they should be implemented in Angular? -


sorry if seems stupid or simple question little confused, have been looking many different kinds of tutorials angular understand concept , how create application.

the issue how attach controller page, have seen 2 methods:

  1. add controller script page
  2. display controller inside app.js website routing is.

here have @ moment please let me know if there issues in 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 home page"; }]); 

again new angular.

updated 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 home page"; }]); 

nope, code fine. use 2 different files app.js routing options , controller.js file different controllers. single file seems bit cluttered me. single file per controller works see usercases turns out few lines of code per page me, can if have extensive codes in each controller


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -