Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了angularjs指令教程大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我是 angularjs的新手,我想了解指令的作用,但我找不到一个复杂的不同示例的教程,如果我可以在指令中移动以下代码,我很好奇.

// 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,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();
    })();

解决方法

如果你对此感兴趣,我写了一个 blog entry not too long ago about the basics of directives.

至于将你拥有的东西转换为指令,它并不太疯狂.

要做的只是使用你已经拥有的代码,但是注入$window而不是使用窗口. (主要用于测试目的).我还添加了一张支票,以确保它没有被应用两次.

所以它看起来像这样:

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,// Detect if this is running as a fullscreen app from the homescreen
          fullscreen = $window.navigator.standalone,lastWidth = 0;

        if (android) {
            // Android's browser adds the scroll position to the innerHeight.
            // Thus,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,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();
        })();
     }
   };
});

要应用它,您会发现之前应用它的#page元素:

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

……那应该是真的.假设您运行的代码,它应该以相同的方式运行.

大佬总结

以上是大佬教程为你收集整理的angularjs指令教程全部内容,希望文章能够帮你解决angularjs指令教程所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: