0

I am trying to access the window dimensions with AngularJS 1.3 to ultimately calculate the orientation of the screen.

None of the examples I find online works and I am not sure why.

angular
    .module('myApp')
    .controller("IntroCtrl", function ($scope, $window) {

        var window = angular.element($window);

        console.log('WindowWidth ' + window.innerWidth);
});

The console reports:

"WindowWidth undefined"

What is the correct way to read the values?

1
  • are you minifying the code? Commented May 6, 2015 at 9:14

2 Answers 2

1

try using $inject instead of angular.element

angular
    .module('myApp')
    .controller("IntroCtrl", function ($scope, $injector) {

        var window = $injector.get("$window");

        console.log('WindowWidth ' + window.innerWidth);
});
Sign up to request clarification or add additional context in comments.

1 Comment

shouldn't it be $injector ?
1
angular
    .module('myApp', [])
    .controller("IntroCtrl", ['$scope', '$window',function ($scope, $window) {
        console.log('WindowWidth ' + $window.innerWidth);
}]);

This will work. http://jsfiddle.net/HB7LU/13496/. Working fiddle for the same code

4 Comments

Sorry but this does not seem to work. Still get innerWidth as undefined.
Its coming for me in the fiddle. Could you check again and see if you missed something out
It does work in fiddle, but cannot get it to work in my application. Will try to find out why.
cool. This provides you with a better solution, so check whats going wrong

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.