自定义指令内的ng-repeat

 似水年华的梦想_818 发布于 2023-01-11 19:39

所以我正在努力将范围传递给我的自定义指令.

这是我的HTML

  • 这是我的指示

    app.directive('archiveNotes', function(){
    return {
        restrict: 'E',
        transclude: true,
        scope: {
            notes: '@',
            paths: '@'
        },
        controller: function($scope) {
    
        },
        templateUrl: '/wp-content/themes/twentythirteen/js/angular/templates/notes.html'
    }
    })
    app.directive('archiveFolders', function(){
    return {
        require: '^archiveNotes',
        restrict: 'E',
        transclude: true,
        scope: {
            path: '@'
        },
        link: function(scope, element, attrs) {
    
        },
        templateUrl: '/wp-content/themes/twentythirteen/js/angular/templates/folders.html'
    }
    });
    

    这是我的模板.

    notes.html
    
    {{post.notes}}
    folders.html
    {{path}}

    我把几件事留空了,i.e controller and link因为在这一点上我只是想在我开始操作DOM之前先弄清楚如何让所有东西都显示出来

    我按照angularjs文档中的示例进行操作,这让我感到非常兴奋.我想似乎无法弄清楚如何访问范围?

    任何帮助表示赞赏.

    1 个回答
    • 根据您的模板,您的archiveNotes指令定义似乎应该如下所示:

      app.directive('archiveNotes', function(){
      return {
          ...
          scope: {
              post: '='
          },
          ...
      }
      })
      

      要获取postng-repeat范围传入的变量,还需要post在指令元素上设置属性:

      <archive-notes post="post"></archive-notes>
      

      同样,您需要在子指令上设置它:

      app.directive('archiveFolders', function(){
      return {
          ...
          scope: {
              post: '='
          },
          ...
      }
      });
      

      ...并更改您的notes.html模板:

      <archive-folders post="post"></archive-folders>
      

      隔离范围有点像防火墙,您可以在其中设置异常,在本例中为特定范围变量.我们在这里所做的就是在指令定义中设置这些异常,然后使用元素上的属性传递它们.

      John Lindquist的这些视频真的为我提供了一些关于隔离范围的视频:

      了解隔离范围

      隔离范围'@'

      隔离范围'='

      隔离范围'&'

      2023-01-11 19:40 回答
    撰写答案
    今天,你开发时遇到什么问题呢?
    立即提问
    热门标签
    PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
    Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有