AngularJS +打字稿:不能注入$ routeProvider [英] AngularJS + TypeScript: cannot inject $routeProvider

查看:113
本文介绍了AngularJS +打字稿:不能注入$ routeProvider的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想申请路由到我的打字稿基础棱角的应用程序。该应用程序应该得到$ routeProvider注射了code是这样的:

I'm trying to apply routing to my Typescript-based Angular application. The app should get $routeProvider injected with a code like this:

var app = angular.module("MyApp", ["ui.bootstrap"]);
// app.service's and controller's here...

app.config(["$routeProvider",
    function ($routeProvider: ng.IRouteProvider) {
        $routeProvider
            .when("/", {
                controller: MyApp.Controllers.ItemsController,
                templateUrl: "/Items.html"
            })
            // ... other routes ...
            .otherwise({
                redirectTo: "/"
            });
}]);

反正当我启动应用程序,我得到的角度告诉我,它无法找到名为提供商的异常的 $ routeProviderProvider 的:

Error: Unknown provider: $routeProviderProvider <- $routeProvider at Error (<anonymous>)
    at http://.../Scripts/angular.js:2734:15
    at Object.getService [as get] (http://.../Scripts/angular.js:2862:39)
    at http://.../Scripts/angular.js:2739:45
    at getService (http://.../Scripts/angular.js:2862:39)
    at invoke (http://.../Scripts/angular.js:2880:13)
    at Object.instantiate (http://.../Scripts/angular.js:2914:23)
    at $get (http://.../Scripts/angular.js:4805:24)
    at $get.i (http://.../Scripts/angular.js:4384:17)
    at forEach (http://.../Scripts/angular.js:137:20) undefined angular.js:5754

通过在角源(1.0.7)偷看,我可以告诉这个来自一个事实,即在2737线,其中的 instanceInjector 的创建,它的名字来源于一个附加命名变量的 providerSuffix 的,其值是提供者,所请求的提供程序名称(这里是$ routeProvider)。从而,这导致异常。然而,正确的名称应该是正确的$ routeProvider如果我在code把它变成只是$路线,如预期,像现在内置名称为$ routeProvider这个错误消失;但我得到另一个异常告诉我,服务$路线没有定义。那么,究竟应该怎样做才能解决此问题?

By peeking at the angular source (1.0.7), I can tell this comes from the fact that at line 2737 where the instanceInjector is created, its name comes from appending a variable named providerSuffix, whose value is "Provider", to the requested provider name (here "$routeProvider"). Thus, this results in an exception. Yet, the correct name should right be "$routeProvider"; if I change it into just "$route" in my code, this error disappears as expected, as now the built name is "$routeProvider"; but I get another exception telling me that the service "$route" is not defined. So, what should I do to resolve this?

推荐答案

更新答案:
就像我在下面

Updated answer: Like I mentioned in the comments below

例如。这将是在控制器无效。仅供参考,你所谈论的ProviderProvider的部分仅仅是记录,而不是它是如何在内部搜索的依赖

e.g. it would be invalid in a controller. FYI The section that you are talking about "ProviderProvider" is just for logging, not how it is internally searching for the dependency

而在你的code发现:

And found in your code:

export class MainController {
    static $inject = ["$scope", "$routeProvider"];

    constructor(private $scope: IMainScope,
        private $routeProvider: ng.IRouteProvider) {
    }
}

不能有routeProvider在控制器。你的app.config为什么是给你的错误。控制器更改为和错误消失:

you cannot have routeProvider in your controllers. Your app.config is not what is giving you the error. Change your controller to and the error goes away:

export class MainController {
    static $inject = ["$scope"];

    constructor(private $scope: IMainScope) {
    }
}

此外使用控制器时,我建议不要实现自己的范围。这里的原因: http://www.youtube.com/观看v = WdtVn_8K17E和放大器;配备= youtu.be&amp; T公司= 5m36s

Additionally I recommend not implementing your own scope when using controllers. Here's why: http://www.youtube.com/watch?v=WdtVn_8K17E&feature=youtu.be&t=5m36s

祝你享尽角+打字稿像我这样:)尽可能多的

Hope you enjoy angular + typescript as much as I do :)

这篇关于AngularJS +打字稿:不能注入$ routeProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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