5
我正在寫一個指令測試,執行測試模板(這是正確加載)時,只呈現爲<!-- ng-repeat="foo in bar" -->
對於初學者的相關部分代碼:
測試
...
beforeEach(inject(function ($compile, $rootScope, $templateCache) {
var scope = $rootScope;
scope.prop = [ 'element0', 'element1', 'element2' ];
// Template loading, in the real code this is done with html2js, in this example
// I'm gonna load just a string (already checked the problem persists)
var template = '<strong ng-repeat="foo in bar"> <p> {{ foo }} </p> </strong>';
$templateCache.put('/path/to/template', [200, template, {}]);
el = angular.element('<directive-name bar="prop"> </directive-name>');
$compile(el)(scope);
scope.$digest(); // <--- here is the problem
isolateScope = el.isolateScope();
// Here I obtain just the ng-repeat text as a comment
console.log(el); // <--- ng-repeat="foo in bar" -->
}));
...
指令
該指令是相當簡單的,它是沒有問題的(外測試一切正常就好了):
app.directive('directiveName', function() {
return {
restrict : 'E',
replace : true,
scope : {
bar : '='
},
templateUrl : '/path/to/template', // Not used in this question, but still...
});
的詳細原因:
- 該指令,測試外,正常工作
- 如果我將模板更改爲如下簡單的東西:
<h3> {{ bar[0] }} </h3>
測試工作正常 - rootScope是l oaded正確
- 的isolateScope結果
undefined