0

如果我所有的模板都在我index.html,每個自己的<script type="text/ng-template" id="foo">...</script>塊裏面,我怎麼可以讓他們進入$templatecache,使茉莉花知道他們存在嗎?

目前,Jasmine將我所有的templateUrl值視爲它們都是遠程路徑。我查看了karma-ng-html2js-preprocessor,但看起來這是爲了將離散模板文件轉換爲$templatecache而不是腳本塊。

這裏是a plunker顯示如何加載內聯模板。

+0

任何理由不使他們的獨立文件?您目前如何在每條路線或指令中檢索模板? 'template'需要一個字符串,'templateUrl'需要一個url,所以你如何得到它們?發表一個例子? – deitch 2014-12-07 14:30:17

+0

@deitch - 我會更新這個問題。這個特定的項目是在離子文件,不適用於離散文件。我可以用Gulp把它們放到$ templateCache中,但是我想盡可能多地保留這個項目的'庫存'(Ionic不支持在其中進行測試),所以我不會完全排除它)。也就是說,我實際上喜歡保持模板內聯,因爲它使事情變得簡單並且可以加快UI的速度。 – Andrew 2014-12-07 14:39:34

+0

我想聽聽具有更多經驗的角度測試角色。我可以看到有1000個外部文件減慢了速度,但5-10個模板並不太重要。無論如何,它們在第一次請求後被緩存。 – deitch 2014-12-07 14:40:39

回答

0

使用karma-ng-template-preprocessor模塊:

預處理採取有<script type="ng/template"> HTML文件,並把它們放入AngularJS模板緩存由於因緣-NG-html2js預處理器爲理念和代碼段

例如,建立karma.conf.js如下:

// preprocess HTML files before serving them to phantomJS 
    preprocessors: {'app/**/*.html': 'ng-template'}, 
// include HTML files in the karma session 
    files: ['**/*.html'], 

// if you have defined plugins explicitly, add karma-ng-template-script-to-template-cache-preprocessor 
// plugins: ['karma-ng-template-preprocessor'] 

ngTemplatePreprocessor: { moduleName: 'inlineTemplates'} 

用它在你的T EST序列爲這樣的:

function foo() 
    { 
    beforeEach(module('inlineTemplates'));  
    } 

describe('example test', foo); 

參考