ui.router,$ scope和controllerAs

时间:2016-11-08 04:38:59

标签: angularjs angular-ui-router

我无法让以下工作,我不断收到Unknown provider错误。有什么想法吗?

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

app.config(function($stateProvider, $urlRouterProvider) {

    $urlRouterProvider.otherwise("/");

    $stateProvider
        .state("home", {
            url : "/",
            templateUrl : "resources/static/views/home.html",
            controller: "HomeCtrl",
            controllerAs: "homeCtrl"
        }
    );
});

app.controller("HomeCtrl", ["$scope", function ($scope) {

    var _this = this;

    // do stuff

}]);

完全错误:

angular.1.5.8.min.js:118 Error: [$injector:unpr] http://errors.angularjs.org/1.5.8/$injector/unpr?p0=<div ui-view="" class="ng-scope">copeProvider%20%3C-%20%24scope%20%3C-%20HomeCtrl
    at Error (native)
    at http://localhost:8080/resources/scripts/js/include/angular.1.5.8.min.js:6:412
    at http://localhost:8080/resources/scripts/js/include/angular.1.5.8.min.js:43:174
    at Object.d [as get] (http://localhost:8080/resources/scripts/js/include/angular.1.5.8.min.js:40:432)
    at http://localhost:8080/resources/scripts/js/include/angular.1.5.8.min.js:43:236
    at d (http://localhost:8080/resources/scripts/js/include/angular.1.5.8.min.js:40:432)
    at e (http://localhost:8080/resources/scripts/js/include/angular.1.5.8.min.js:41:158)
    at Object.instantiate (http://localhost:8080/resources/scripts/js/include/angular.1.5.8.min.js:42:24)
    at http://localhost:8080/resources/scripts/js/include/angular.1.5.8.min.js:90:32
    at Object.<anonymous> (http://localhost:8080/resources/scripts/js/include/angular-ui-router.0.3.1.min.js:7:23872)

link

和html

<div>Home</div>

为清楚起见,我需要使用controllerAs,因此删除它不是一种选择。

编辑:必须是ui.router版本0.3.1中的错误,当我切换到0.3.2时它工作正常。

1 个答案:

答案 0 :(得分:1)

您在index.html中缺少data-ui-view

<body data-ng-app="myApp">
  <h2>AngularJS Ui router - Demonstration</h2>
  <div data-ui-view=""></div>
</body>

控制器:

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

}]);

<强> DEMO