我想測試一個使用templateURL的angularJS指令。對我來說,我無法讓編譯器實際加載templateURL,即使它已經放入templateCache中。我意識到業力預處理所有的模板內容,併爲每個預先載入templateCache的模塊創建模塊,但我預計這將是等效的。測試指令與模板的URL
這裏是一個http://jsfiddle.net/devshorts/cxN22/2/演示怎麼回事。
angular.module("app", [])
.directive("test", function(){
return {
templateUrl:"some.html",
replace:true
}
});
//--- SPECS -------------------------
describe("template url test", function() {
var element, scope, manualCompiledElement;
beforeEach(module('app'));
beforeEach(inject(function($rootScope, $controller, $compile, $templateCache){
$templateCache.put("some.html", "<div>hello</div>");
scope = $rootScope.$new();
element = $compile(angular.element('<test></test>'))(scope);
manualCompiledElement = $compile(angular.element($templateCache.get('some.html')))(scope);
scope.$digest();
}));
it("has hello", function() {
expect(element.text()).toContain('hello');
});
it("template cache has contents", function(){
expect(manualCompiledElement.text()).toContain('hello');
});
});
我在想什麼?
好變化,我從來沒有能夠想出解決辦法。我轉而使用業力,現在一切正常。 – devshorts
我即將放棄 - 這是關於模板緩存+茉莉花的事情。我可以讓它與模板一起工作。請注意,您在指令中缺少'restrict:'E''。默認情況下它是'restrict:'A'',並且您在元素的上下文中使用它,而不是屬性。既然你有工作,我不會浪費更多的時間。 –
感謝您爲@WordsLikeJared拍攝一個照片。我不確定業力預處理器在做什麼不同(它看起來幾乎與我完全相同),但使用業力無論如何都是正確的選擇,因爲它提供了其他便利的功能。那個,每個人都用它來達到這個目的。我猜想最好能游泳 – devshorts