如何在AngularJS中将$ window对象注入config中

编程入门 行业动态 更新时间:2024-10-24 12:33:49
本文介绍了如何在AngularJS中将$ window对象注入config中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将$window对象注入AngularJS的config方法中,但是我一直遇到错误...

I am trying to inject the $window object into the config method in AngularJS, but I keep getting an error...

正确的方法是什么?

这是我的代码:

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]; } })

实时演示

推荐答案

您不能将$window服务注入到配置中,因为服务尚未在配置时初始化.但是,您可以向它们注入提供程序并获取实例.在您的情况下:

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); })

更多推荐

如何在AngularJS中将$ window对象注入config中

本文发布于:2023-11-16 20:50:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1607416.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中将   对象   如何在   config   AngularJS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!