Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Below is my code

HTML
<div class="border_box"></div>
<div id="MainDiv">
     <div id="ObjAccordian" ng-repeat="guide in GuideDetails" init="refresh();">
                                ---my code 
     </div>
</div>


I want to get the Height of MainDiv and set the height to BorderBox Height
Posted
Updated 28-Oct-15 0:01am
v2

1 solution

You could use ng-style with a function and parse the selector of the div to copy:

HTML
<script type="text/javascript">
var myApp = angular.module('myApp',[])

myApp.controller('myController'['$scope',function($scope){
   $scope.getHeight = function(selector){
      var element = $(selector); //You will prolly need to get the first item
      return "height:"+element.css("height")+";"; //should read "height:100px;" for eg
   };
}]);

</script>

<div class="border_box"></div>
<div id="MainDiv" ng-style="getHeight('.border_box')">
     <div id="ObjAccordian" ng-repeat="guide in GuideDetails" init="refresh();">
                                ---my code 
     </div>
</div>



I eventually ended up putting a lot of this sort of thing into a service which I call pretty often. I use very customizes Bootstrap so things don't always line up.

Just to show you, this is my service:
JavaScript
angular.module("HtmlStyleModule", [])
    .service("styleService", [
        function () {

            this.getStyleWidth = function (element) {
                if (element) {
                    if (element.element)
                        if (element.element.css("padding")) {
                            var a = element.element.css("padding").match(/\d+(\.(\d)+)?/)[0];
                            var b = element.element.css("width").match(/\d+(\.(\d)+)?/)[0];
                            return { "width": (b - (a * 2)) + "px" };
                        }
                }
                return "";
            }
        }
    ]);


I use this one directive defined elements, which is why I don't use the selector method:
HTML
<div kendo-window="confirmMoneyPageAdd" k-title="'Add MoneyPage'" k-width="1000" k-height="600" k-visible="false" k-modal="true">
        --my fields
        <div class="btn-group btn-group-justified btn-bottom" ng-style="getStyleWidth(confirmMoneyPageAdd)">
            <div class="btn btn-default" data-ng-click="winAddMoneyPageClick()">Confirm (Add new)</div>
            <div class="btn btn-default" data-ng-click="confirmMoneyPageAdd.close()">Cancel</div>
        </div>
    </div>


I hope you find that helpful ^_^
Andy

UPDATE: I was trying to be too much of a smart arse. here is the simple solution:

XML
    <script type="text/javascript">
        var myApp = angular.module('myApp', []);
        myApp.controller('myController', ['$scope', function ($scope) {

            $scope.GuideDetails = [1, 2, 3, 4, 5];
            angular.element(document).ready(function () {
                $('.border_box').css('height', $('#MainDiv').height());
                $scope.$apply();
            });
        }]);
    </script>
</head>
<body ng-app="myApp" ng-controller="myController">
    <div class="border_box"></div>
    <div id="MainDiv">
        <div id="ObjAccordian" ng-repeat="guide in GuideDetails" init="refresh();">
            <input type="text" />
        </div>
    </div>
</body>
 
Share this answer
 
v2
Comments
Rajni from delhi 28-Oct-15 6:42am    
Hi,
I want to get the height of Maindiv which dynamically set according to the Ng-repeat element counts. and then set that height to Borderbox div.
you do the reverse of it.I am not able to get the height of Maindiv which is coming 0px.
Andy Lanng 28-Oct-15 6:49am    
Ah, right. In my case I am getting the details for the style. You will have to get the height directly.
Try using element.offsetHeight
so the line would read:
return "height:"+element.offsetHeight+"px;";

I'm not sure how well that will work with JQuery selector. It definitely will work with an 'element' that angular passes through. let me know if it doesn't work and I'll see if I can write up a directive for you.
Rajni from delhi 28-Oct-15 7:36am    
No.... undefined is coming.now my code Looks like
<div class="border_box" ng-style="getHeight('#MainDiv')"></div>
<div id="MainDiv">
<div id="ObjAccordian" ng-repeat="guide in GuideDetails" init="refresh();">
---my code
</div>
<div>

$scope.getHeight = function (selector) {
var element = $(selector); //You will prolly need to get the first item
alert(element.offsetHeight);

return "height:" + element.offsetHeight + ";"; //undefined is coming
};
Andy Lanng 28-Oct-15 7:38am    
I thought it might :/
I'll see if I can write you a directive. It might have to wait about an hour if that's ok?
Rajni from delhi 28-Oct-15 7:41am    
yah.. fine for me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900