In Internet Explorer 8, angular's $window service may sometimes return undefined

I've developed a custom directive called slideshow with some scope variables that are two-way bound to ng-style. This directive functions as a responsive carousel that adjusts based on the window height retrieved using the $window service.

During testing in an IE8 virtual machine, I encountered an issue where the ng-style did not apply any styles in IE8 due to the $window service being undefined. Any suggestions on how to resolve this and make it function properly?

Display:

This feature works flawlessly on modern browsers:

JavaScript: slideshow directive

'use strict';

angular.module('alexApp')
  .directive('slideshow', function($window){
  return {
      restrict: 'A',
      templateUrl: 'template1.html',
      controller: function($scope, Pages) {
        $scope.adPageData = Pages;
        $scope.adpageLength = $scope.adPageData.length;
      },
      link: function postLink(scope, element, attrs) {


       var targetRatio = 0.8419689;
       var navigationOffset = 92;
       var currentPageIndex = 0;

       scope.initializeWindowSize = function() {
         scope.pageCollectionWidth = $window.innerWidth;
         scope.pageCollectionHeight = $window.innerHeight;
         scope.pageCollectionWidthTotal = scope.pageCollectionWidth + 'px';
         scope.properPageWidth = ( scope.pageCollectionHeight-navigationOffset)*targetRatio + 'px';

         scope.properPageWidthLength = ((scope.pageCollectionHeight-navigationOffset)*targetRatio)*scope.adpageLength + 'px';
         scope.leftPageOffset = scope.pageCollectionHeight/4+'px';
         scope.currentPageOffset = currentPageIndex*(-(scope.pageCollectionHeight-navigationOffset)*targetRatio)+'px';

       }

       scope.initializeWindowSize();
        //alert('initi');


       return angular.element($window).bind('resize', function() {
        scope.initializeWindowSize();

        //alert('resized');
        return scope.$apply();


      });
      }
  };
});     

HTML - template1.html:

<script type="text/ng-template" id="template1.html">
    <div class="weekly-viewport" ng-style="{width:pageCollectionWidthTotal}">
        <ul class="page-collection" ng-style="{width:properPageWidthLength, left:leftPageOffset, marginLeft:currentPageOffset }">
         <li class="page-layout" ng-repeat="page in adPageData.pages" ng-style="{width:properPageWidth}">
              <ul class="page shop-local-page">
                  <li class="imageurl"><img ng-src="{{page.imageurl}}" alt="" /></li>
              </ul>
          </li>
      </ul>
    </div>
  </script>

Sample pages factory

var app = angular.module('alexApp');
app.factory('Pages', function() {

    var Pages = {};
    Pages = {
      pages': [
            {
            imageurl: 'http://placekitten.com/200/300'
            },
            {
            imageurl: 'http://placekitten.com/200/300'
            }
          }
      }
  });

Answer №1

The issue is not the absence of the $window service - instead, it is related to IE8's inadequate support for window.innerHeight/Width.

If you are utilizing jQuery, consider using $(window).height()/width().
For those not using jQuery, here is a workaround for IE:

scope.pageCollectionWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
scope.pageCollectionHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Dynamically assigning a name to a variable through code

Is there a quicker way to complete these 100 tasks? variable_1 = 1; variable_2 = 2; variable_3 = 3; ... variable_100 = 100; I attempted to use the following code: for(var i = 1; i <= 100; i++) { variable_ + i = i; } However, I encountered an e ...

What is the best way to arrange an array of objects in JavaScript by numerical order and then alphabetically?

Possible Duplicate: Sorting objects in an array by a field value in JavaScript I'm looking to sort an array of objects both numerically (by id) and alphabetically (by name). However, the current method I'm using is not giving me the desired ...

Utilizing hyperlinks to dynamically remove elements from a webpage with the power of HTML5 and JavaScript

Looking for guidance on how to create a link that will remove two specific list items from an unordered list located above the link. As a beginner, any assistance is greatly appreciated! ...

Elevate the React experience by smoothly sliding a fixed navbar upwards when scrolling down, and elegantly sliding it

tl;dr Don't miss the solution at the end! Looking to create a slide up and down effect on a fixed navbar in react? What's the best approach - using refs or the componentDidMount lifecycle hook? hideNav = (navbar) => { const hide = () ...

Navigating through a React application with several workspaces - the ultimate guide

Currently, I am working on implementing a monorepo setup inspired by this reference: https://github.com/GeekyAnts/nativebase-templates/tree/master/solito-universal-app-template-nativebase-typescript In this repository, there are 4 distinct locations wher ...

Could one potentially generate new static files in Nextjs without needing to rebuild the entire app?

After recently beginning to utilize NextJs' getStaticProps feature, I have found that the static files generated at build time are quite impressive. However, my content is not static and requires updates without having to rebuild the entire app each t ...

"Exploring the Basics: Diving into Node.js, angular.js, and MongoDB - Uncovering Relationship Modeling and Essential Tips

Transitioning from the Java and relational database world, I am diving into a new project involving an appointment scheduling system using node.js and MongoDB on the backend with Angular.js on the client side. I'm seeking clarification on a few key c ...

Disabling the ripple effect on the primary action in React Material lists: A Step-by-Step

I was attempting to include two action buttons at the opposite ends of a list component. https://i.stack.imgur.com/juv8F.gif When clicking on the secondary action (delete icon on the right), the ripple effect is confined to just the icon. On the othe ...

Creating a responsive design for mobile apps in Ionic using CSS

I need help with making my Ionic app responsive across all mobile devices. The design on the page was created using CSS, but it is not displaying properly on every device. How can I ensure that it adapts to different screen sizes? <template> <Io ...

What could be causing my ASP.Net MVC script bundles to load on every page view?

I'm a bit puzzled. The _layout.cshtml page I have below contains several bundles of .css and .js files. Upon the initial site load, each file in the bundles is processed, which makes sense. However, every time a new view is loaded, each line of code f ...

What is the best method to extract data from a specific member of a list within a dictionary with AngularJS?

Within my dictionary, the structure is as follows : Dictionary<string,List<B>> a; The class B has the following format: public class B { public Int64 c; public Boolean d; } Upon performing backend processing, I have obtained a wit ...

What is the process for eliminating the invocation of a function in Jquery?

I am currently facing an issue with my application. When I launch the Ficha() function, it initiates an ajax call and works perfectly fine. However, another ajax call is made later to load HTML tags that also need to invoke the Ficha() function. The prob ...

Issue: .catch(error) function in Node / Express not returning as expectedDescription: After

I'm currently developing a REST API and focusing on effectively managing all error scenarios. Upon successful completion of the API call, I make sure to return the success object to the calling function and then send the response to the client. Howev ...

Why is only the peak of the wave visible? I am eager to showcase the full extent of its beauty

Having an issue with the appearance of a superposed wave using threejs. When displayed, the wave made from plane material only shows the upper half, but when turned upside down using mouse dragging, it appears correctly. // Turn the wave plane upside down ...

Change the background image when hovering over an element with vanilla JavaScript by utilizing data attributes

I would like to be able to change the background image of a <div> when hovering over a link. Initially, the <div> should not have a background image when the page loads for the first time. This is my current setup: <div class="background"& ...

Populate object values dynamically through function invocations

Currently, I am involved in a project with a VueJS application that incorporates the following helper class and method: class BiometricMap { static get(bioType) { if (!bioType) { return BiometricMap.default(); } const bioTypes = { ...

Having trouble uploading images from my personal computer onto Phaser 3

Currently in the process of coding a game for a school project using Phaser. I am quite new to Phaser and have very little knowledge of JavaScript. Despite searching online for potential solutions, none seem to be effective for me. I have included my cod ...

Issue with Karma and angular-mocks: The error message "TypeError: 'undefined' is not an object (evaluating 'angular.mock = {}')" is being shown

I am facing an issue while writing unit tests using Karma + Jasmine. The problem arises with angular-mocks when I run grunt test, resulting in the following error message: PhantomJS 1.9.8 (Mac OS X) ERROR TypeError: 'undefined' is not an ob ...

What could be causing the invalid hooks error to appear in the console?

I'm completely stumped as to why this error is popping up when I try to use mutations with React Query. Any insights or advice would be greatly appreciated. Note: I'm implementing this within a function component in React, so it's puzzling ...

geolocation data not being updated in isolate scope binding

I'm currently facing an issue with using isolated scope in a directive. Surprisingly, everything seems to be working perfectly fine. I have set the '=' scope on two properties and successfully bind them in my template. However, when I call m ...