2013-05-15 67 views
4

我想在加載後更改iframe的scrollTop。 這是我的想法。如何在angularJS中使用iframe

模板

<iframe .. onload="{{loaded()}}" ..></iframe> 

在控制器

$scope.loaded = function() { 
    //chage scroll of iframe 
} 

但我認爲這是沒有棱角的風格( 「請不要將任何類型的DOM操作」)

什麼是最好的做法做這個?

+0

指令將是最好的實踐即 –

回答

10

這裏是mycode。但跨域不工作..

directive('myIframe', function(){ 
    var linkFn = function(scope, element, attrs) { 
     element.find('iframe').bind('load', function (event) { 
      event.target.contentWindow.scrollTo(0,400); 
     }); 
    }; 
    return { 
     restrict: 'EA', 
     scope: { 
     src:'@src', 
     height: '@height', 
     width: '@width', 
     scrolling: '@scrolling' 
     }, 
     template: '<iframe class="frame" height="{{height}}" width="{{width}}" frameborder="0" border="0" marginwidth="0" marginheight="0" scrolling="{{scrolling}}" src="{{src}}"></iframe>', 
     link : linkFn 
    }; 
    }); 
+1

這可能是跨域破壞的原因。 https://github.com/angular/angular.js/issues/3679 – Nick

+1

我知道這是一個老問題,但要擺脫跨域錯誤,您可以進行更改:'return {... scope.src:' &src'...}','template:'