Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9191431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:40:54+00:00 2026-06-17T20:40:54+00:00

I am new to angularjs and I would like to understand what the directives

  • 0

I am new to angularjs and I would like to understand what the directives do but I can’t find a tutorial with different example by complexity and I was curios if I could move the following code in a directive.

    // hide the url bar 
    var page = document.getElementById('page'),
    ua = navigator.userAgent,
    iphone = ~ua.indexOf('iPhone') || ~ua.indexOf('iPod'),
    ipad = ~ua.indexOf('iPad'),
    ios = iphone || ipad,
    // Detect if this is running as a fullscreen app from the homescreen
    fullscreen = window.navigator.standalone,
    android = ~ua.indexOf('Android'),
    lastWidth = 0;

    if (android) {
        // Android's browser adds the scroll position to the innerHeight.
        // Thus, once we are scrolled, the page height value needs to be corrected in case the     page is loaded
        // when already scrolled down. The pageYOffset is of no use, since it always
        // returns 0 while the address bar is displayed.
        window.onscroll = function () {
            page.style.height = window.innerHeight + 'px'
        }
    }
    var setupScroll = window.onload = function () {
        // Start out by adding the height of the location bar to the width, so that
        // we can scroll past it
        if (ios) {
            // iOS reliably returns the innerWindow size for documentElement.clientHeight
            // but window.innerHeight is sometimes the wrong value after rotating
            // the orientation
            var height = document.documentElement.clientHeight;
            // Only add extra padding to the height on iphone / ipod, since the ipad
            // browser doesn't scroll off the location bar.
            if (iphone && !fullscreen) height += 60;
            page.style.height = height + 'px';
        } else if (android) {
            // The stock Android browser has a location bar height of 56 pixels, but
            // this very likely could be broken in other Android browsers.
            page.style.height = (window.innerHeight + 56) + 'px'
        }
        // Scroll after a timeout, since iOS will scroll to the top of the page
        // after it fires the onload event
        setTimeout(scrollTo, 0, 0, 1);
    };
    (window.onresize = function () {
        var pageWidth = page.offsetWidth;
        // Android doesn't support orientation change, so check for when the width
        // changes to figure out when the orientation changes
        if (lastWidth == pageWidth) return;
        lastWidth = pageWidth;
        setupScroll();
    })();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:40:55+00:00Added an answer on June 17, 2026 at 8:40 pm

    I wrote a blog entry not too long ago about the basics of directives if you’re interested in that.

    As far as converting what you have there into a directive, it’s not too crazy.

    All you would do is use the code you already have, but inject $window instead of using window. (Mostly for testing purposes). I also added a check to make sure it didn’t get applied twice.

    So it would look a little something like this:

    app.directive('windowResizeThingy', function($window) {
       return {
         restrict: 'A',
         link: function(scope, elem, attr) {
    
           // make sure this doesn't get applied twice.
           if($window.windowResizeThingyApplied) return;
           $window.windowResizeThingyApplied = true;
    
            // hide the url bar 
            var page = elem[0],
              ua = $window.navigator.userAgent,
              iphone = ~ua.indexOf('iPhone') || ~ua.indexOf('iPod'),
              ipad = ~ua.indexOf('iPad'),
              ios = iphone || ipad,
              // Detect if this is running as a fullscreen app from the homescreen
              fullscreen = $window.navigator.standalone,
              android = ~ua.indexOf('Android'),
              lastWidth = 0;
    
            if (android) {
                // Android's browser adds the scroll position to the innerHeight.
                // Thus, once we are scrolled, the page height value needs to be corrected in case the     page is loaded
                // when already scrolled down. The pageYOffset is of no use, since it always
                // returns 0 while the address bar is displayed.
                window.onscroll = function () {
                    page.style.height = window.innerHeight + 'px'
                }
            }
            var setupScroll = $window.onload = function () {
                // Start out by adding the height of the location bar to the width, so that
                // we can scroll past it
                if (ios) {
                    // iOS reliably returns the innerWindow size for documentElement.clientHeight
                    // but window.innerHeight is sometimes the wrong value after rotating
                    // the orientation
                    var height = document.documentElement.clientHeight;
                    // Only add extra padding to the height on iphone / ipod, since the ipad
                    // browser doesn't scroll off the location bar.
                    if (iphone && !fullscreen) height += 60;
                    page.style.height = height + 'px';
                } else if (android) {
                    // The stock Android browser has a location bar height of 56 pixels, but
                    // this very likely could be broken in other Android browsers.
                    page.style.height = (window.innerHeight + 56) + 'px'
                }
                // Scroll after a timeout, since iOS will scroll to the top of the page
                // after it fires the onload event
                setTimeout(scrollTo, 0, 0, 1);
            };
            ($window.onresize = function () {
                var pageWidth = page.offsetWidth;
                // Android doesn't support orientation change, so check for when the width
                // changes to figure out when the orientation changes
                if (lastWidth == pageWidth) return;
                lastWidth = pageWidth;
                setupScroll();
            })();
         }
       };
    });
    

    And to apply it, you’d find your #page element you were applying it to before:

     <div id="page" window-resize-thingy></div>
    

    … and that should be it really. Presuming the code you have works, it should be run pretty much the same way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to implement promises in my AngularJS application like you can see
I am relatively new to AngularJS and would like to know how feasible it
I am trying to connect AngularJS with MongoDB using Mongoose. I would like to
I have an xml-like text, in which I would like to find the item
I am new to node. I very much like approach of AngularJS. I like
New to python, competent in a few languages, but can't see a 'snazzy' way
I'm new to AngularJS and perhaps I'm going about this the wrong way, but
new to c#. I'm trying to make a simple system where I can search
New to Node.js and Express, I am trying to understand the two seems overlapping
New programmer here, I am trying to understand and break down this code below

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.