ui.router,$ scope和controllerAs [英] ui.router, $scope, and controllerAs

查看:45
本文介绍了ui.router,$ scope和controllerAs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法执行以下操作,我不断收到未知提供程序错误.有什么想法吗?

I am unable to get the following to work, I keep getting an Unknown provider error. Any ideas?

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,因此将其删除不是一种选择.

For clarity, I need to use controllerAs, so removing it is not an option.

ui.router的0.3.1版本中一定有一个错误,当我切换到0.3.2时,它可以正常工作.

Must be a bug in version 0.3.1 of ui.router, when I switched to 0.3.2 it worked fine.

推荐答案

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

You are missing data-ui-view in 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和controllerAs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆