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

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

问题描述

我无法使以下内容正常工作,我不断收到 Unknown provider 错误.有什么想法吗?

var app = angular.module('app', ['ui.router']);app.config(function($stateProvider, $urlRouterProvider) {$urlRouterProvider.otherwise("/");$stateProvider.state(家",{网址:/",templateUrl : "资源/静态/视图/home.html",控制器:HomeCtrl",控制器为:homeCtrl"});});app.controller("HomeCtrl", ["$scope", function ($scope) {var _this = this;//做东西}]);

完全错误:

angular.1.5.8.min.js:118 错误:[$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在错误(本机)在 http://localhost:8080/resources/scripts/js/include/angular.1.5.8.min.js:6:412在 http://localhost:8080/resources/scripts/js/include/angular.1.5.8.min.js:43:174在 Object.d [as get] (http://localhost:8080/resources/scripts/js/include/angular.1.5.8.min.js:40:432)在 http://localhost:8080/resources/scripts/js/include/angular.1.5.8.min.js:43:236在 d (http://localhost:8080/resources/scripts/js/include/angular.1.5.8.min.js:40:432)在 e (http://localhost:8080/resources/scripts/js/include/angular.1.5.8.min.js:41:158)在 Object.instantiate (http://localhost:8080/resources/scripts/js/include/angular.1.5.8.min.js:42:24)在 http://localhost:8080/resources/scripts/js/include/angular.1.5.8.min.js:90:32在对象<匿名>(http://localhost:8080/resources/scripts/js/include/angular-ui-router.0.3.1.min.js:7:23872)

link

和 html

首页

为清楚起见,我需要使用 controllerAs,因此无法将其删除.

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

解决方案

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

<h2>AngularJS Ui 路由器 - 演示</h2><div data-ui-view=""></div>

控制器:

var myApp = angular.module("myApp", ['ui.router']);myApp.config(function($stateProvider, $urlRouterProvider) {$urlRouterProvider.when("", "/home");$stateProvider.state(家",{网址:/家",templateUrl: "home.html",控制器:'HomeCtrl',控制器为:家"});});myApp.controller('HomeCtrl', ['$scope', function($scope) {var vm = 这个;vm.hello = "演示";}]);

演示

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

}]);

Full Error:

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)

And link

And html

<div>Home</div>

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

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

解决方案

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>

Controller:

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天全站免登陆