AngularJS – window resize and angular get element height in controller

AngularJS – window resize and angular get element height in controller

Example 1 : angular get element height in controller

In this Post We Will Explain About is AngularJS – window resize and angular get element height in controller With Example and Demo.Welcome on infinityknow.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to angular get element height in controllerExample

In this post we will show you Best way to implement angularjs watch element height window innerheight AngularJS, hear for AngularJS – How to get window height in angular jswith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

READ :  Set textarea directive value in Angular

Include External Libs

angular-1.0.0.js  // https://code.angularjs.org/1.0.0/angular-1.0.0.js

Step 1 : HTML Part

<div ng-app="liveApp" ng-controller="resizeCtrl" ng-style="style()" resize>your Browse window.height: {{windowHeight}}
    <br />your Browser window.width: {{windowWidth}}
    <br />
<h1>BIGGEST AND FASTEST Web Tutorials </h1>

<p>We are Web Technology Experts and Team who provide you very Important information on Web Development information,Examples and Demo, Interview Questions and Answers, live project problem solution and their solution and online free tutorials – “infinityknow.com”.</p>

<p><mark>You can download free 100% source code and Demo from below link.</mark></p>

https://infinityknow.com/

https://infinityknow.com/tutorial
</div>

Step 2 : javascript Part

var liveApp = angular.module('liveApp', []);

function resizeCtrl($scope) {
    /* Logic here or some angular code goes here */
}

liveApp.directive('resize', function ($window) {
    return function (scope, element) {
        var w = angular.element($window);
        scope.getWindowDimensions = function () {
            return {
                'h': w.height(),
                'w': w.width()
            };
        };
        scope.$watch(scope.getWindowDimensions, function (customval, oldValue) {
            scope.windowHeight = customval.h;
            scope.windowWidth = customval.w;

            scope.style = function () {
                return {
                    'height': (customval.h - 100) + 'px',
                        'width': (customval.w - 100) + 'px'
                };
            };

        }, true);

        w.bind('resize', function () {
            scope.$apply();
        });
    }
});

Step 3 : CSS Part

div {
    border:2px solid green;
}

Example

READ :  Dynamically Set navbar active tab angularjs ngclass

Example 2 : angularjs set element height in controller

HTML Part (index.html)

<!DOCTYPE html>
<html>
  <head>
    <script  src="http://code.angularjs.org/1.2.13/angular.js"></script>
  </head>
  <body ng-app="liveApp">
    <h1 my-directive>Hello infinityknow.com ! width: {{width}}, height: {{height}}
    </h1>
  </body>
</html>

index.js

angular.module('liveApp', [])
  .directive('myDirective', function($timeout) {
    return {
        restrict: 'A',
        link: function(scope, element) {
            scope.height = element.prop('offsetHeight');
            scope.width = element.prop('offsetWidth');
        }
    };
  })
;

You can download free 100% source code and Demo from below link.

Example

I hope you have Got What is angularjs get window height and width examples And how it works.I would Like to have FeadBack From My Blog(infinityknow.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(infinityknow.com) Are Most Always Welcome.

Add a Comment

Your email address will not be published. Required fields are marked *