angularjs – 如何`inject“$window`对象到`config`在角
发布时间:2020-12-17 08:27:21 所属栏目:安全 来源:网络整理
导读:我试图注入$窗口对象配置方法在角,但我不断收到错误.. 什么是正确的方法来做到这一点? 这里是我的代码: angular.module('myApp',['$window']) //is this wrong? .config(function ($window) { //this is not the way? console.log($window); //console.lo
我试图注入$窗口对象配置方法在角,但我不断收到错误..
什么是正确的方法来做到这一点? 这里是我的代码: 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]; } }) Live Demo
你不能注入$ window服务到配置,因为服务未在配置时初始化。但是,您可以注入它们的提供程序并获取一个实例。在你的情况:
angular.module('myApp',[]) .config(function ($windowProvider) { var $window = $windowProvider.$get(); console.log($window); }) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |