Angularjs 在md虚拟重复中使用角度指令
我正在尝试使用列表项的自定义指令实现Angular Material的虚拟重复,但在将scope attrs绑定到该指令时遇到了问题 以下是我所拥有的: HTMLAngularjs 在md虚拟重复中使用角度指令,angularjs,angular-material,Angularjs,Angular Material,我正在尝试使用列表项的自定义指令实现Angular Material的虚拟重复,但在将scope attrs绑定到该指令时遇到了问题 以下是我所拥有的: HTML <md-virtual-repeat-container id="vertical-container"> <div md-virtual-repeat="item in ctrl.dynamicItems"> <foo bar="item"></foo> </di
<md-virtual-repeat-container id="vertical-container">
<div md-virtual-repeat="item in ctrl.dynamicItems">
<foo bar="item"></foo>
</div>
</md-virtual-repeat-container>
JS
myApp.directive("foo", function(){
return {
restrict: "E",
scope: {
bar: "="
},
template: "<span>{{baz}}</span>",
link: function(scope){
scope.baz = "new " + scope.bar;
}
}
myApp.directive(“foo”,function()){
返回{
限制:“E”,
范围:{
酒吧:“=”
},
模板:“{baz}}”,
链接:功能(范围){
scope.baz=“新建”+scope.bar;
}
}
问题
虚拟滚动部分中的项目似乎在那里,但它们的值显示为newundefined
,而我希望它们是新的1、新的2,依此类推
奇怪的是,有些项目看起来确实正确地显示了它们的值(即new 11、new 12,有时还有new 13)。此外,如果我将指令的模板替换为“{bar}”,这个问题就会得到解决
认为指令的链接功能不必要。这让我认为问题在于链接的时间(这是我的最佳猜测)
这里有一个链接到,以获得更多的澄清
有什么想法吗?终于解决了-
JS
指令(“foo”,函数(){
返回{
限制:“E”,
范围:{
酒吧:“=”
},
模板:“{test()}}”,
链接:功能(范围){
scope.test=函数(){
如果(定义了角度(范围条)){
返回“new”+scope.bar;
}
}
}
}
.directive("foo", function(){
return {
restrict: "E",
scope: {
bar: "="
},
template: "<span>{{test()}}</span>",
link: function(scope){
scope.test = function () {
if (angular.isDefined(scope.bar)) {
return "new " + scope.bar;
}
}
}
}