Angularjs ui-router non-controller abstract state

I am new to UI-Routerand struggling to call my controller. I am using an abstract state as a parent, and it does not seem to call the controller. The following is a simplified version of my code.

I bound $ stateChangeError to the console and did not receive an error on the console. I have added the onEnter and OnExit attributes to my state and only the onEnter function is called (not included below).

index.html

<!DOCTYPE html>
<html ng-app="plunker">

<head>
  <meta charset="utf-8" />
  <title>AngularJS Plunker</title>
  <script>document.write('<base href="' + document.location + '" />');    </script>
  <link rel="stylesheet" href="style.css" />
  <script data-require="angular.js@1.3.x"  src="https://code.angularjs.org/1.3.15/angular.js" data-semver="1.3.15">   </script>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.14/angular-ui-router.js" ></script>
 <script src="app.js"></script>
</head>

<body>

   <div ui-view="navbar" ></div>

   <div class="container">
     <div ui-view="content" ></div>
   </div>

   <p>We are on the homepage.</p>

</body>
</html>

App.js

var app = angular.module('plunker', ['ui-router']);

app.config(function ($stateProvider, $urlRouteProvider) {
  $urlRouteProvider.otherwise("/");

  $stateProvider.state('site', {
    abstract: true,
    views: {
      'navbar@': {
        template: '<p>This is the {{nav}}</p>',
        controller: 'NavBarCtrl'
      }
    }
  });
});
app.config(function($stateProvider) {
  $stateProvider
    .state('home', {
      parent: 'site',
      url: '/',
      views: {
        'content@': {
          template: '<p>This is the {{content}}</p>',
          controller: 'MainCtrl'
        }
      }
    });
});      
app.controller('NavBarCtrl', function($scope) {
  $scope.nav = 'Navbar'
});    
app.controller('MainCtrl', function($scope) {
  $scope.content = 'content'
});
+4
source share
1 answer

There is a working example

There are two questions. First, we need to specify the correct module:

// instead of this
// var app = angular.module('plunker', ['ui-router']);
// we need this
var app = angular.module('plunker', ['ui.router']);

And we must use proper names '$urlRouterProvider'

// instead of this
app.config(function ($stateProvider, $urlRouteProvider) {
  $urlRouteProvider.otherwise("/");

// we need this
app.config(function ($stateProvider, $urlRouterProvider) {
  $urlRouterProvider.otherwise("/");

Check here

+2

All Articles