2014-01-09 15 views
6

我分析了一堆的電子郵件從服務器,我現在想在網頁上顯示它們。我得到了他們的HTML內容,並且我認爲IFrame是顯示電子郵件的最簡單的方式,因爲它們是要顯示的。AngularJS和IFrame的srcdoc

然而,

<iframe srcdoc="{{ email.html }}" frameborder="0"></iframe> 

給了我下面的AngularJS錯誤:

Error: [$interpolate:interr] Can't interpolate: {{ email.html }} 
Error: [$sce:unsafe] Attempting to use an unsafe value in a safe context. 

我一直在尋找一種方式來做到這一點,嘗試禁用$ SCE作爲測試,但沒」不管工作。這只是一個測試項目,我得到的數據是安全的,我只需要一個POC。

我在控制器現在這樣做:

var iframeDocument = document.querySelector('#myiframe').contentWindow.document; 
var content = $scope.email.html; 
iframeDocument.open('text/html', 'replace'); 
iframeDocument.write(content); 
iframeDocument.close(); 

這一工程,但我還是更喜歡通過數據綁定做到這一點,如果有一個解決方案?謝謝。

+0

什麼是email.html? – dandavis

+0

@dandavis包含實際電子郵件內容的HTML文檔 – cabaret

+0

嘗試首先將文檔轉換爲字符串,最終將srcDoc設置爲html字符串。 – dandavis

回答

2

嘗試添加ngSanitize作爲應用程序的依賴項。

這裏你有更多的信息:http://docs.angularjs.org/api/ngSanitize

+0

這幫助過我。我已經從我的index.html引用了angular-sanitize.js,但我需要將它作爲依賴項添加進去:angular.module('app',['ngSanitize']); –

6

添加過濾器,使信任值:

angular.module('app').filter('toTrusted', ['$sce', function($sce) { 
    return function(text) { 
     return $sce.trustAsHtml(text); 
    }; 
}]); 

,然後應用過濾器:

<iframe srcdoc="{{email.html | toTrusted}}" frameborder="0"></iframe> 

完整代碼:http://jsfiddle.net/2bvktbLr/1/