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.

2

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

2

You should use location provider to routes across.

$location.path( 'views/about.html');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.