AngularJS窗口尺寸
我试图使用AngularJS 1.3访问窗口尺寸,以最终计算屏幕的方向 我在网上找到的例子都不管用,我也不知道为什么AngularJS窗口尺寸,angularjs,Angularjs,我试图使用AngularJS 1.3访问窗口尺寸,以最终计算屏幕的方向 我在网上找到的例子都不管用,我也不知道为什么 angular .module('myApp') .controller("IntroCtrl", function ($scope, $window) { var window = angular.element($window); console.log('WindowWidth ' + window.innerWidth)
angular
.module('myApp')
.controller("IntroCtrl", function ($scope, $window) {
var window = angular.element($window);
console.log('WindowWidth ' + window.innerWidth);
});
控制台报告:
"WindowWidth undefined"
读取值的正确方法是什么 尝试使用$inject而不是angular.element
angular
.module('myApp')
.controller("IntroCtrl", function ($scope, $injector) {
var window = $injector.get("$window");
console.log('WindowWidth ' + window.innerWidth);
});
这会有用的。为同一代码工作fiddle是否应该是
$injector
?您是否正在缩小代码?抱歉,这似乎不起作用。仍然得到未定义的内部宽度。它在小提琴中向我走来。你能不能再检查一下,看看你是否遗漏了什么东西?它确实可以用小提琴演奏,但不能用在我的应用程序中。我会努力找出原因的。酷。这为您提供了一个更好的解决方案,所以请检查出了什么问题
angular
.module('myApp', [])
.controller("IntroCtrl", ['$scope', '$window',function ($scope, $window) {
console.log('WindowWidth ' + $window.innerWidth);
}]);