手动引导前采用了棱角分明的服务 [英] Using angular services before manual bootstrap

查看:92
本文介绍了手动引导前采用了棱角分明的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢做手工引导(不使用的NG-应用指令)之前,使用角度的服务。有没有调用angular.bootstrap上的空白div来访问角度服务的一种方式?

下面的方法是有效的,但我不知道是否还有一个更直接的解决方案:

  VAR元素= angular.element('< D​​IV>< / DIV>');
  angular.bootstrap(元件);
  变量$ =喷油element.injector();
  变量$ HTTP = $ injector.get('$ HTTP');
  变量$位置= $ injector.get('$位置');


解决方案

有没有办法做到这一点不调用引导(或正从一个已自举元素注入),但有一个稍微更直接的方式这样做:

 变量$ HTTP = angular.bootstrap()得到('$ HTTP')。

或者,如果你需要多个服务,你可以这样做:

  angular.bootstrap()调用(函数($ HTTP,$位置){
    $ http.get('/ foo的');
    $ location.path('/ foo的');
});

I would like to use angular services before doing a manual bootstrap(the ng-app directive is not being used). Is there a way to access angular services without calling angular.bootstrap on an empty div?

The following approach works, but I'm wondering if there is a more direct solution:

  var element = angular.element('<div></div>');
  angular.bootstrap(element);
  var $injector = element.injector();
  var $http = $injector.get('$http');
  var $location = $injector.get('$location');

解决方案

There isn't a way to do it without calling bootstrap (or getting the injector from an already bootstrapped element), but there is a slightly more direct way of doing it:

var $http = angular.bootstrap().get('$http');

Or, if you need multiple services, you can do this:

angular.bootstrap().invoke(function($http, $location){ 
    $http.get('/foo');
    $location.path('/foo');
});

这篇关于手动引导前采用了棱角分明的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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