从喷油器访问$位置 [英] Accessing $location from injector

查看:129
本文介绍了从喷油器访问$位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此刻的一台路由器的简单应用angularjs:

I have a simple angularjs application with a single router at the moment :

angular.module('myApp', ['ngRoute'])
            .config(['$routeProvider', function($routeProvider) {
                $routeProvider.when('/editFilter/:id', {templateUrl: '/webapp/template/filterDetail'});
            }])

我的目标是调用editFilter模板时,我需要它。
我试图找回那样的$位置:

My goal is to call the editFilter template when I need it. I am trying to retrieve the $location like that :

angular.injector(['myApp']).get('$location')

但它失败,

错误:[$喷油器:unpr]
  的http://errors.angularjs.org/1.2.1/$injector/unpr?p0=%24rootElementProvider%20%3C-%20%24rootElement%20%3C-%20%24location%20%3C-%20%24route

Error: [$injector:unpr] http://errors.angularjs.org/1.2.1/$injector/unpr?p0=%24rootElementProvider%20%3C-%20%24rootElement%20%3C-%20%24location%20%3C-%20%24route

如果我称之为 angular.injector(['对myApp'])。获得('$位置')则返回true。

If I call angular.injector(['myApp']).get('$location') it returns true.

什么我做错了任何想法?我有尝试几种解决方法,但每次我结束了一个非常类似的错误。

Any idea of what I am doing wrong? I have try several workaround but everytime I end up with a very similar error.

推荐答案

如果你会读<一个href=\"http://docs.angularjs.org/error/%24injector%3aunpr?p0=%24rootElementProvider%20%3C-%20%24rootElement%20%3C-%20%24location%20%3C-%20%24route\"相对=nofollow>错误页面你看,你忘了,包括一定的相关性。请不要忘记,包括他们。一个现在失败为$ rootElement的,因为你可以在错误读取。包括这一次,然后再次尝试,看看其他的依赖关系应包括在内。

If you would have read the error page you see that you forgot to include certain dependencies. Please don't forget to include them. The one failing now is $rootElement, as you can read in the error. Include this one, and try again to see if other dependencies should be included.

编辑:
 好吧,我错了,有的看完后我发现这个方法:

OK, I was wrong, after some reading I noticed that this method:

angular.injector(['myApp']).get('$location')

创建一个新的喷油器实例。这是不是你想要的,我认为。
因此,我不知道什么是你的情况,但如果你是在AngularJS仅仅注入$喷油器实例。如果不是在AngularJS电话

creates a new injector instance. This is not what you want I think. So I don't know what is your case but if you are in AngularJS just inject the $injector instance. If not in AngularJS call

$injector = angular.element([DOM element]).injector();
$injector.get('$location');

这篇关于从喷油器访问$位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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