ui.router,$范围和controllerAs(ui.router, $scope, and

2019-09-30 11:15发布

我无法得到以下工作,我不断收到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)

和链接

和HTML

<div>Home</div>

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

编辑:必须在ui.router的0.3.1版中的错误,当我切换到0.3.2它工作得很好。

Answer 1:

这是因为丢失data-ui-view中的index.html,

<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



文章来源: ui.router, $scope, and controllerAs