2014-09-17 1 views
7

나는필자의 경우 iframe 콘텐츠 높이를 설정하는 방법은 무엇입니까?

$scope.init = function(id) { 
      console.log($('#'+ id)) -> show my iframe 
      var x= $('#'+ id)..contentWindow.document.body.scrollHeight; 
      console.log(x) -> gives me undefined 

      var y = $('#'+ id)..contentWindow; 
      console.log(y) -> give me undefined too 
     } 

가 어떻게 내 컨트롤러를 통해 iframe이 콘텐츠 높이를 설정하는 내 컨트롤러에

<iframe id='iframe' width='100%' height='600px' data-ng-init="init('iframe')" 
src='http://test.com' /> 

같은 뭔가를

를 통해 설정에 iframe이 콘텐츠 높이를 노력하고 있어요?

감사합니다. 코드에서

+0

'100 %'에 iframe이 높이를 설정하고 당신이 어떤 평소에 따라 제어 부모 DIV에 포장 메서드 (예 : CSS). –

+0

DOM 조작은 지시문 내에서 수행되어야합니다. 예를 들어 http://plnkr.co/edit/KgrhJg7xKQqxm9pho4SH?p=preview – miqid

답변

8

일부 관찰 : https://docs.angularjs.org/api/ng/directive/ngInit :

  1. ng-init$(window).on("load", function(){...})의 equivalient에 대한 ng-init 여기에 자세한 내용이 아닙니다. 따라서 코드가 실행될 때 iframe이 아직로드되지 않았기 때문에 을 xy으로 가져 오는 것입니다.

  2. 각도에서 컨트롤러에서 DOM에 액세스하는 것은 좋지 않습니다. 대신 지시문에서 이러한 종류의 작업을 수행하는 것이 좋습니다.

  3. angularjs로 시작하는 경우 jQuery를 사용하지 않는 것이 좋습니다.

당신이 원하는 것은 iframeSetDimentionsOnload과 같은 지시자를 정의하고 거기에 높이를 설정하는 것입니다. 몇 분 안에 예를 들어 드릴 것입니다.

귀하의 iframeSetDimensionsOnload 지침 :

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); 
     }) 
    } 
}}]) 

이처럼 사용

<iframe iframe-set-dimensions-onload src='http://test.com' /> 
+0

@FlyingCat을보고 최신 업데이트를보고 문제가 해결되는지 알려주세요. 감사! – Josep

+0

고마워요. Josep – FlyingCat

+0

콘솔에서 높이를 얻으려고하면 '원산지 프레임 차단'http : // localhost : 3000 "이 교차 원산지 프레임에 액세스하지 못합니다. 지시문은 이벤트 리스너가 기록하기 전에 onload가 실행되지 않습니다. console.log. –

관련 문제