How to set the iframe content height in my case?
Some observations from your code:
ng-init
is not the equivalient of$(window).on("load", function(){...})
, more information aboutng-init
here: https://docs.angularjs.org/api/ng/directive/ngInit . That's why you are gettingundefined
forx
andy
, because when that code is executed the iframe hasn't been loaded yet.In angular is not a good idea to access the DOM from the controller, consider doing those sort of operations in a directive instead.
If you are starting with angularjs I would recommend you to try not to use jQuery.
In your case I think that what you want is to define a directive like iframeSetDimentionsOnload
and set the height there. I will give you in example in a few minutes.
Your iframeSetDimensionsOnload
directive:
yourApp.directive('iframeSetDimensionsOnload', [function(){
return {
restrict: 'A',
link: function(scope, element, attrs){
element.on('load', function(){
/* Set the dimensions here,
I think that you were trying to do something like this: */
var iFrameHeight = element[0].contentWindow.document.body.scrollHeight + 'px';
var iFrameWidth = '100%';
element.css('width', iFrameWidth);
element.css('height', iFrameHeight);
})
}
}}])
Use it like this:
<iframe iframe-set-dimensions-onload src='http://test.com' />