最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get element parent height inside directive in AnguarJS - Stack Overflow

programmeradmin0浏览0评论

How do you get and set the parent height from an element from inside a directive?

This is what I have now, obviously it's not working.

var vAlign = angular.module("vAlign", [])
.directive('vAlign', function() {
  return {
        restrict : "AC",
        link: function(scope, e){

            e.parent.height(1200);
            console.log(e.parent.height);
        }
    };
});

How do you get and set the parent height from an element from inside a directive?

This is what I have now, obviously it's not working.

var vAlign = angular.module("vAlign", [])
.directive('vAlign', function() {
  return {
        restrict : "AC",
        link: function(scope, e){

            e.parent.height(1200);
            console.log(e.parent.height);
        }
    };
});
Share Improve this question asked Aug 5, 2015 at 17:25 Raymond the DeveloperRaymond the Developer 1,6765 gold badges27 silver badges59 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 11

You can use parent and height methods of jqLite/jQuery:

link: function(scope, e) {
    e.parent().height(1200);
    console.log(e.parent().height());
}

Or you could do it in pure javascript with parentNode property, which is a reference to parent element:

link: function(scope, e) {
    e[0].parentNode.style.height = 1200 + 'px';
}

Also note, that since e is an jqLite/jQuery instance here which is a array-like collection of one single element, you need to use [0] to access raw HTMLElement.

e.parent is a function, so you must call it as such:

e.parent().height(1200);

Further, if you do not load jquery on the page, you will have to use

.css('height', '1200px')

instead, as jqLite does not include .height

发布评论

评论列表(0)

  1. 暂无评论