加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs指令教程

发布时间:2020-12-17 17:57:39 所属栏目:安全 来源:网络整理
导读:我是 angularjs的新手,我想了解指令的作用,但我找不到一个复杂的不同示例的教程,如果我可以在指令中移动以下代码,我很好奇. // hide the url bar var page = document.getElementById('page'),ua = navigator.userAgent,iphone = ~ua.indexOf('iPhone') || ~
我是 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>

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读