【问题标题】:Using angular directive within md-virtual-repeat在 md-virtual-repeat 中使用 angular 指令
【发布时间】:2016-11-30 12:37:40
【问题描述】:

我正在尝试使用列表项的自定义指令来实现 Angular Material 的虚拟重复,但我遇到了将范围属性绑定到指令的问题。

这是我所拥有的:

HTML

<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; 
      }
    }

问题

虚拟滚动部分中的项目似乎在那里,但它们的值显示为 new undefined,而我希望它们是 new 1new 2 em> 等等。

奇怪的是,有些项目似乎正确地显示了它们的值(即,new 11new 12,有时还有 new 13)。此外,如果我将指令的模板替换为"&lt;span&gt;{{bar}}&lt;/span&gt;",则问题得到解决,这认为指令的链接功能是不必要的。这使我认为问题出在链接的时间上(这是我最好的猜测)。

这里是corresponding CodePen snippet 的链接以获取更多说明。

有什么想法吗?

【问题讨论】:

    标签: angularjs angular-material


    【解决方案1】:

    终于解决了-CodePen

    JS

      .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;
              }
            }
          }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-26
      • 2018-05-09
      • 1970-01-01
      • 1970-01-01
      • 2017-02-08
      • 2017-05-17
      • 1970-01-01
      • 2016-11-15
      相关资源
      最近更新 更多