Angularjs ui-router抽象状态不调用控制器

Taa*_*sem 5 javascript angularjs angular-ui-router

我是新手,UI-Router并且正在努力调用我的控制器.我使用抽象状态作为父,它似乎没有调用控制器.下面是我的代码的简化版本.

我将$ stateChangeError绑定到控制台上,并且没有在控制台上出错.我将onEnter和OnExit属性添加到我的状态,只调用onEnter函数(不包括在下面).

的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>
Run Code Online (Sandbox Code Playgroud)

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'
});
Run Code Online (Sandbox Code Playgroud)

Rad*_*ler 2

一个工作示例

有两个问题。首先我们必须引用正确的模块:

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

我们必须使用正确的名称'$urlRouterProvider'

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

// we need this
app.config(function ($stateProvider, $urlRouterProvider) {
  $urlRouterProvider.otherwise("/");
Run Code Online (Sandbox Code Playgroud)

检查它在这里工作