2015-04-01 52 views
0

我認爲它已經很好地放在標題中了,但是我想要做的是從$ templateCache.get()獲得模板內容;我試圖依賴注入$ templateCache到app.config的不同部分如何從templates.js文件獲取app.config的內容templateCache.get?

... 
... 
.config(['$routeProvider', '$templateCache', function($routeProvider, $templateCache) { 

$routeProvider. 
    when('/', { 
     template : function ($templateCache) {return $templateCache.get('main.html');} 
    }). 

這導致$ templateCache不是函數。

我的app.js是它自己的模塊,templates.js在app.js文件之後的索引文件中被鏈接。 Template.js文件是由一個Grunt任務創建的,它將內容填充到他們所關注的模板中。

我在這裏看到一些問題,有templateCache迭代,並從那裏把內容放到app.run部分templateCache所以我quess我的問題是我是否可以以某種方式使用我的template.js使用app.run把( )到$ templateCache中,還是必須重構我的Grunt任務以將其構建到json對象中?任何plnkr和/或資源都會有所幫助。抱歉含糊不清。

回答

2

爲什麼不直接使用tempalte的templateCache Id(它是filepath/filename,如果它是html2js-ified)?如果匹配的templateCache鍵存在,Angular將自動從templateCache加載它:

$routeProvider. 
    when('/', { 
     templateUrl : 'main.html' 
    }) 
+0

它還不存在。我有一個單獨的角度模塊上的模板 - templates.js,但它會在app.js文件/模塊後調用。我已經嘗試了你所建議的以及我在Q中所擁有的內容,但是他們(預計會如此)給出錯誤,因爲templateCache未分配或者templateCache鍵不存在。寫這個,我意識到從ngRoute改爲ngUiRouter不會解決它。我應該只是將編譯好的模板編譯成JSON文件並從中填充templateCache? – 2015-04-07 05:47:50