本文介绍了如何``inject` $ window`反对在角的`config`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图注入 $窗口对象配置在角的方法,但我一直得到错误..

I am trying to inject the $window object to config method in angular, But i am keep getting error..

什么是做到这一点的正确方法?

what is the correct way to do this?

这是我的code:

angular.module('myApp', ['$window']) //is this wrong?

  .config(function ($window) { //this is not the way?
      console.log($window); //console.log fails //error
  })

  .controller("main", function($scope) {
    $scope.index = 0;
    $scope.num = number[$scope.index];

   $scope.increase = function () {
     $scope.index += 1;
     $scope.num = number[$scope.index];
   }
})

推荐答案

您不能因为服务没有在配置时初始化尚未注入 $窗口服务的配置。但是,你可以注入他们的供应商,并得到一个实例。你的情况:

you can't inject $window service to the config as services are not initialized at config time yet. however, you can inject them providers and get an instance. in your case:

angular.module('myApp', [])

 .config(function ($windowProvider) {
   var $window = $windowProvider.$get();
   console.log($window);
 })

这篇关于如何``inject` $ window`反对在角的`config`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 17:00