AngularJS +路由+解析 [英] AngularJS + Routing + Resolve

查看:101
本文介绍了AngularJS +路由+解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这个错误:

错误:错误:[$喷油器:unpr]的http://errors.angularjs.org/1.3.7/$injector/unpr?p0=Htt$p$psponseProvider%20%3C-%20Htt$p$psponse%20%3C-%20DealerLeads

Error: Error: [$injector:unpr] http://errors.angularjs.org/1.3.7/$injector/unpr?p0=HttpResponseProvider%20%3C-%20HttpResponse%20%3C-%20DealerLeads

未知喷油提供商

下面是我的路由器(ui.router):

Here's my router (ui.router):

$stateProvider
        .state('main', {
            url: "/main",
            templateUrl: "views/main.html",
            data: { pageTitle: 'Main Page' }
        })
        .state('leads', {
            url: "/leads",
            templateUrl: "views/leads.html",
            data: { pageTitle: 'Dealer Leads' },
            controller: 'DealerLeads',
            resolve: DealerLeads.resolve
        })

下面是我的控制器:

function DealerLeads($scope, HttpResponse) {
    alert(JSON.stringify(HttpResponse));
}

下面是我的决心:

DealerLeads.resolve = {
    HttpResponse: function ($http) {
...
    }
}

中的数据越来越向控制器,我看到它的警报。但是,控制器完成后,认为(我认为)的渲染过程中,这个问题似乎发生。

The data is getting to the controller, I see it in the alert. However, after the controller is done, during the rendering of the view (I think), the issue seems to be happening.

最终渲染视图有两个控制器:身体标记的一个主控制器,和第二控制器DealerLeads'里面。我试着删除主控制器,这个问题仍是present。

The final rendered view has two controllers: One main controller in the body tag, and the second controller 'DealerLeads' inside of that. I've tried removing the main controller, and the issue is still present.

我是什么做错了吗?是否有更多的code,它是必要的了解/解决这个问题?

What am I doing wrong? Is there any more code that is necessary to understand/resolve the issue?

推荐答案

当您使用路线的决心参数作为绑定到该路由控制器依赖注入,则无法使用该控制器与 NG-控制器指令,因为与名称服务提供商的Htt presponse 不存在。它是一个动态的依赖由路由器时,它实例在各自的局部视图要绑定的控制器注入。

When you use route resolve argument as dependency injection in the controller bound to the route, you cannot use that controller with ng-controller directive because the service provider with the name HttpResponse does not exist. It is a dynamic dependency that is injected by the router when it instantiates the controller to be bound in its respective partial view.

只需删除 NG-控制器=DealerLeads从视图中,并确保这一观点是由国家线索呈现的HTML的一部分 @ templateUrl:意见/ leads.html,。路由器将其绑定到模板为你解决动态依赖的Htt presponse 。如果你想使用controllerAs您可以指定在路由器本身: -

Just remove the ng-controller="DealerLeads" from the view and make sure that view is part of the html rendered by the state leads @ templateUrl: "views/leads.html",. Router will bind it to the the template for you resolving the dynamic dependency HttpResponse. If you want to use controllerAs you can specify that in the router itself as:-

controller: 'DealerLeads',
controllerAs: 'leads' //Not sure if this is supported by ui router yet

controller: 'DealerLeads as leads',

此外,当你这样做:

Also when you do:

.state('leads', {
        url: "/leads",
        templateUrl: "views/leads.html",
        data: { pageTitle: 'Dealer Leads' },
        controller: 'DealerLeads',
        resolve: DealerLeads.resolve
    })

确保 DealerLeads 是该路由定义的地方访问。这将是一个更好的做法是在路由定义移动到它自己的控制器文件,使他们都在同一个地方。并尽可能尤其是这是更好地摆脱 NG-控制器的启动指​​令,而使用路由实例并绑定该控制器为模板的路线的局部视图。它只用其合同给出了视图不紧密耦合与控制器名称的全方面更加重用性和替代。所以我不会担心删除 NG-控制器指令,其中路由器可以实例化控制器。

make sure that DealerLeads is accessible at the place where the route is defined. It would be a better practice to move the route definition to its own controller file so that they are all in one place. And whenever possible especially in a partial view of a route it is better to get rid of ng-controller starting the directive and instead use route to instantiate and bind the controller for that template. It gives more re-usability in terms of the view as a whole not being tightly coupled with a controller name and instead only with its contract. So i would not worry about removing ng-controller directive where router can instantiate the controller.

这篇关于AngularJS +路由+解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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