Bomani Kernizan Bomani Kernizan - 1 month ago 19
AngularJS Question

UI routes not working

I can't seem to get my angular app to work with routes. The basic app itself works but breaks the moment I try to add in routes

Here's a link to the plunker: broken routes

var app = angular.module('appModule', ['ui.router']).config(['$stateProvider', '$urlRouterProvider',function($stateProvider, $urlRouterProvider) {

$urlRouterProvider.otherwise('/home');

$stateProvider
.state('home', {
url: '/home',
templateUrl: 'home.html'
controller: 'homeController'})}])

app.controller('homeController', ['$scope', function($scope) {$scope.hello = "flarg";}])


and here is the the same simple app without routes link to plunker here: working app

Answer

You configuration should be like this,

var myApp = angular.module("myApp", ['ui.router']);
myApp.config(function($stateProvider, $urlRouterProvider) {
  $urlRouterProvider.when("", "/home");
  $stateProvider
    .state("home", {
      url: "/home",
      templateUrl: "home.html",
      controller: 'homeController'
    });
});
myApp.controller('homeController', ['$scope', function($scope) {
  $scope.hello = "flarg";
}]);

DEMO

Comments