0

I have an issue were my routing is not populating my view.

I have the following code running from an Xampp local server:

angular 1.4.9

index.html:

<html>
  <head>
    <script src="scripts/angular.js" type="javascript/text"></script>
    <script src="scripts/Ng-Route.js" type="javascript/text"></script>
    <script src="scripts/app.js" type="javascript/text"></script>
  </head>
  <body ng-app="myApp">
  <div ng-view></div>
  </body>
</html>

user.html:

<div>
  <fieldset>
    Hello, {{ctrl.message}}
  </fieldset>
</div>

app.js:

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

app.config(function($routeProvider) {
    $routeProvider
        .when('/', {
            templateUrl : 'user.html',
            controller  : 'controller as ctrl'
        });

        $routeProvider.otherwise({ redirectTo: '/' });
});

app.controller('controller', function() {
    var self = this;
    self.message = 'Everyone';
  });

I have absolutely no clue why this is failing, nothing shows up on the page. I am expecting "Hello, Everyone".

Any help would appreciated.

5
  • can you check network and console in developer tools for errors? Commented Feb 21, 2016 at 20:06
  • The console shows no errors Commented Feb 21, 2016 at 20:09
  • Everything looks normal in dev tools, all scripts are loading and no errors are logging. Commented Feb 21, 2016 at 20:12
  • 1
    Works here plnkr.co/edit/PXswypPGy6wuwhiHQbJV?p=preview Commented Feb 21, 2016 at 20:14
  • Thanks for looking @YuryTarabanko, I think it may be an issue with how the scripts are loading locally on my machine Commented Feb 21, 2016 at 20:19

1 Answer 1

1

You had mistaken declaring your controller on your route. controller does accept controller name in string/controller function. And use controllerAs option to alias your controller.

$routeProvider
    .when('/', {
        templateUrl : 'user.html',
        controller  : 'controller',
        controllerAs: 'ctrl'
    });
Sign up to request clarification or add additional context in comments.

3 Comments

Unfortunately this has made no difference, I do prefer this syntax though so thanks.
OPs version actually works. The problem should be somewhere else. plnkr.co/edit/PXswypPGy6wuwhiHQbJV?p=preview
@YuryTarabanko thanks for heads up, I actually looked at this line and suggested that change for OP.

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.