Javascript ngRepeat中的角度指令转换
我对ng repeat指令中的转换有问题。找不到Angular。请在模板中排除元素,以便不会发生替换。我相信这是由于ng repeat在转换时移除了.transclude。我想知道如何在ng repeat拿到占位符之前进行替换,或者如何以任何其他方式解决此问题 旁注:如果改用ng TRANCLUDE指令,则代码为 按预期工作,但我必须使用$parent {{$parent.item.name}来访问我不喜欢的值 以下是我的代码的简化版本: htmlJavascript ngRepeat中的角度指令转换,javascript,angularjs,Javascript,Angularjs,我对ng repeat指令中的转换有问题。找不到Angular。请在模板中排除元素,以便不会发生替换。我相信这是由于ng repeat在转换时移除了.transclude。我想知道如何在ng repeat拿到占位符之前进行替换,或者如何以任何其他方式解决此问题 旁注:如果改用ng TRANCLUDE指令,则代码为 按预期工作,但我必须使用$parent {{$parent.item.name}来访问我不喜欢的值 以下是我的代码的简化版本: html <div mydir="items"&g
<div mydir="items">
{{item.name}}
</div>
<div ng-repeat="item in items">
<div class="transclude"></div>
</div>
编译前的预期结果
app.directive("mydir" , function(){
return {
templateUrl : "template.html",
transclude : true,
scope : {
items: "=mydir"
},
link : function($scope , $element , attrs , $ctrl , $transclude){
$transclude(function($content){
$element.find(".transclude").replaceWith($content);
});
},
};
})
<div mydir="items">
<div ng-repeat="item in items">
{{item.name}}
</div>
</div>
{{item.name}
我认为有一个选项可以帮助您实现目标。基本上,它获取html指令{{item.name}}
的内容,并在指令的编译函数中构建动态模板
var app = angular.module('plunker', []);
app.directive("mydir" , ['$compile', '$templateRequest', function($compile, $templateRequest){
return {
scope : {
items: "=mydir"
},
compile: function($element) {
var transclude = $element.html();
$element.html('');
return function(scope, elem) {
$templateRequest("template.html").then(function(html){
var tpl = angular.element(html);
tpl.children('.transclude').append(transclude);
$compile(tpl)(scope);
elem.append(tpl);
});
};
}
};
}]);
app.controller('MainCtrl', function($scope) {
$scope.items = [{
name: "Item 1"
},{
name: "Item 2"
}]
});
以下是。添加更多的上下文,您正试图实现的目标是什么?这将有助于您找到其他解决方案。我在网上看到了这些解决方案,但我加载模板而不是分配给变量的事实使我无法使用。请使用
$templateRequest
从文件加载它。Plunker已更新。