我正在使用ng-repat指令在我的HTML中放置自定義指令。使用ng-repeat在Angular 1.5.8中放置自定義指令不起作用
但是,自定義指令不被評估,如果直接在HTML上放置相同的指令,那麼它正在工作。
...
<body ng-app="docsSimpleDirective">
<div ng-controller="Controller">
<div>This is a Problem</div>
<!--Here in for loop i want to us the value to call a directive-->
<div ng-repeat="var in arr">
<!--Here i am using directive with restrict: 'C' and this is not expending-->
<span class="{{var}}"></span>
</div>
<!-- here my directive named "direct" is working fine -->
<span class="direct"></span>
</div>
</body>
...
和我的JS代碼是
(function(angular) {
'use strict';
angular.module('docsSimpleDirective', [])
.controller('Controller', ['$scope', function($scope) {
$scope.arr = ['direct','indirect'];
}]).directive('direct', function() {
return {
template: 'direct',
restrict: 'C'
};
}).directive('indirect', function() {
return {
template: 'indirect',
restrict: 'C'
};
});
})(window.angular);
我相信有一些編譯的問題,我搜索網頁,發現$編譯可以解決我的目的,但無法實施。
請幫我解決我的問題。
Plunker FPGA實現的相同:https://plnkr.co/edit/lYGg0UkQpNhN5NJx13Zj?p=preview
它不工作,因爲當角解析器來'<跨度類= 「{{VAR}}」>'其編譯DOM&值評估'{{VAR}}'值,即意思是在DOM編譯之後'class'屬性值被填滿了,這就是爲什麼指令沒有被編譯。你可以通過使用'ng-switch' /'ng-if'指令來解決這個問題,在'ng-if' /'ng-switch'中把兩個指令'direct'&indendirect和一些'expression'放在一起。 –