2015-04-03 95 views
1

是否可以$編譯HTML文檔, - 不是像$compile那樣的元素期望?

問題我試圖解決:我在外部.html文件打印形式,我想包括該行:<link rel="stylesheet" type="text/css" href="styles.css">

現在,我不能這樣做,因爲$compile期待element參數,哪一個是DOM元素並且不能包含(或簡單地忽略到樣式表的鏈接)。

模板我有:

<div> 
    <span ng-bind="model.tasks.count"></span> 
</div> 

模板,我想有(我的主要目標 - 提取樣式成單獨的文件):我的代碼

<html> 
    <head> 
    <link href="/app/style.css" rel="stylesheet"> 
    </head> 

    <body> 
    <div> 
     <span ng-bind="model.tasks.count"></span> 
    </div> 
    </body> 
</html> 

實施例:

return $q.all([ getTemplate(), getScope() ]).then(
    function(msg) { 
     var element = angular.element(msg[0]); // Now it's DOM element 
     $compile(element)(msg[1]); // If i pass HTML document, element goes undefined 
     openWindow(element); 
    }); 

and openWindow:

function openWindow(element) { 
    $timeout(function() { 
     var html = element.html(); 

     var _w = window.open(); 
     _w.document.body.innerHTML = html; // Here i'd like to write complete HTML doc 
     _w.print(); 
    }); 
} 

回答

0

是否可以$編譯HTML文檔, - 不是像$ compile 期望的元素?

這是不是真的正確,$compile期待什麼,你可以通過完整的HTML作爲字符串要麼angular.element$compile。但得到的答覆仍然是沒有,$compile使用jqLit​​e引擎蓋下:

if (!($compileNodes instanceof jqLite)) { 
    // jquery always rewraps, whereas we need to preserve the original selector so that we can 
    // modify it. 
    $compileNodes = jqLite($compileNodes); 
    } 
    // We can not compile top level text elements since text nodes can be merged and we will 
    // not be able to attach scope data to them, so we will wrap them in <span> 
    forEach($compileNodes, function(node, index) { 
    if (node.nodeType == NODE_TYPE_TEXT && node.nodeValue.match(/\S+/) /* non-empty */) { 
     $compileNodes[index] = jqLite(node).wrap('<span></span>').parent()[0]; 
    } 
    }); 

而且jqLit​​e將把HTML文檔方式一樣jQuery() - 作爲頂級元素從<head><body的集合。當然,$compile並非用於編譯整個文檔。

如果您在模板中使用樣式,則可以使用<style type="text/css">@import url("...")</style>

相關問題