Search code examples
javascriptangularjsangularjs-routingangularjs-controlleras

AngularJS variable does not persist to view


Having a strange bug here, I can succesfully switch to other views but my problem is that even a simple variable cannot "reach" the views. Does anyone see my (probably stupid) mistake?

Index.html

<html ng-app="myApp">
<head>
    <title>AngularJS oefening</title>
    <link href="css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
    <div class="container">
        <header>
            <a href="#/home">Home</a>
            <a href="#/about">Over ons</a>
            <a href="#/contact">Contact</a>
        </header>
        <div ng-view>

        </div>
        <footer>
            Copyright...
        </footer>
    </div>

    <script src="js/angular/angular.min.js"></script>
    <script src="js/angular/angular-route.min.js"></script>
    <script src="js/controller.js"></script>
</body>

Controller.js

angular.module('myApp', ['ngRoute'])
    .config(moduleConfig);

moduleConfig.$inject = ['$routeProvider'];

function moduleConfig($routeProvider) {
    $routeProvider.when('/', {
        templateUrl: 'views/home.html',
        controller: 'homeController',
        contollerAs : 'homeCtrl'
    })
    .when('/home', {
        templateUrl: 'views/home.html',
        controller: 'homeController',
        contollerAs : 'homeCtrl'
    })
    .when('/about', {
        templateUrl: 'views/about.html',
        controller: 'aboutController',
        contollerAs : 'aboutCtrl'
    })
    .when('/contact', {
        templateUrl: 'views/contact.html',
        controller: 'contactController',
        contollerAs : 'contactCtrl'
    })
    .otherwise({
        redirectTo: '/'
    });
};

angular.module('myApp')
    .controller('homeController', homeController)
    .controller('aboutController', aboutController)
    .controller('contactController', contactController);

function homeController() {
    this.msg = "Hello home";
};

function aboutController() {
    this.msg = "Hello about";
};

function contactController() {
    this.msg = "Hello contact";
};

Home.html

<div>
    <h1>{{homeCtrl.msg}}</h1>
</div>

I tested with hardcoded text and it gives the right view whenever I use the menu to navigate. I've also tried logging different objects (homeController, homeCtrl, ...). Only the variable this is NOT undefined, but this one only contains the $scope.

Thanks in advance.


Solution

  • After searching for almost 2 hours.. I've found my stupid.. stupid mistake.

    contollerAs -> controllerAs