1

I just started learning some AngularJS and I created a simple routing system with two controllers but my second controller doesn't do anything and I can't seem to fix the problem.

app.js:

angular.module('foodListerApp', [
'ngAnimate',
'ngCookies',
'ngResource',
'ngRoute',
'ngSanitize',
'ngTouch']).config(function ($routeProvider) {
$routeProvider
  .when('/', {
    templateUrl: 'views/main.html',
    controller: 'MainCtrl',
    controllerAs: 'main'
  })
  .when('/about', {
    templateUrl: 'views/about.html',
    controller: 'AboutCtrl',
    controllerAs: 'about'
  })
  .otherwise({
    redirectTo: '/'
  });
});

MainCtrl (main.js):

angular.module('foodListerApp').controller('MainCtrl', ['$scope' , $window , $location ,  function ($scope , $window , $location) {



    console.log("In Main Controller ... ");


    $scope.onSubmit = function() {
        $window.location.href = 'views/about.html';
    }; 
}]);

AboutCtrl (about.js):

angular.module('foodListerApp').controller('AboutCtrl', ['$scope' , function ($scope) {
    console.log("In 'about' controller ..."); 
}]); 

I did not include the HTML files but everything looks good in there and the "onSubmit" method works just fine. The first controller is printing, but the second controller doesn't print anything and I am not sure why that is happening.

Zain
  • 21
  • 2
  • 6

2 Answers2

2

angular.module('foodListerApp').controller('MainCtrl', ['$scope' , $window , $location , function ($scope , $window , $location) {

should be angular.module('foodListerApp').controller('MainCtrl', ['$scope' , '$window' , '$location' , function ($scope , $window , $location) {

you forgot to put quotes around them.

Tj Gienger
  • 1,395
  • 1
  • 12
  • 17
2

You should use location provider to routes across.

$location.path( 'views/about.html');
Deepak Ingole
  • 14,912
  • 10
  • 47
  • 79