Angular中实现树形结构视图实例代码

近两年当中使用Angular开发过很多项目,其中也涉及到一些树形结构视图的显示,最近的在项目中封装了大量的组件,一些组件也有树形结构的展示,所以写出来总结一下。

相信大家都知道,树结构最典型的例子就是目录结构了吧,一个目录可以包含很多子目录,子目录又可以包含若干个子孙目录,那咱们今天就以目录结构为例来说明一下Angular中树结构的实现。

首先,我们希望封装一个组件,用于显示整个目录的树形机构,代码如下:

rush:xhtml;">

就像上面的代码一样,我们直接声明folder-tree标签,将controller中的folder数据作为参数传递进去,预期会显示一个完整的树状目录结构。接下来我们就来定义模块,控制器,以及相应的指令部分:

rush:js;"> angular.module('treeDemo',[]) .controller("TreeController",function($scope) { $scope.folder = { name: 'techs',children: [ { name: 'server-side',children: [ { name: 'Java' },{ name: 'Python' },{ name: 'Node' } ] },{ name: 'front-end',children: [ { name: 'jQuery' },{ name: 'Angular' },{ name: 'React' } ] } ] } }) .directive("folderTree",function() { return { restrict: "E",scope: { currentFolder: '=' },templateUrl: 'template/tree.html' }; });

如上所述,在控制器中我们在$scope中绑定了一个folder的数据对象,包含整个的目录结构数据,接着定义了folderTree指令,它会使用tree.html作为模板进行视图渲染,我们这就来look look模板中的内容

rush:xhtml;">

{{currentFolder.name}}

可以看到,在模板中有个很重要的环节,那就是使用ng-repeat指令遍历当前目录的子集,然后再次使用folder-tree组件来渲染相应的子目录,这种方式简直是完美,现在我们来看看渲染的结果:

这种在模板中嵌套使用指令的方法很完美,只可惜低版本的Angular中是无法实现的,会出现无限递归循环,造成页面假死,这是Angular本身的解析机制造成的。经测试,Angular 1.5.0及以上版本是没有问题的,但在Angular 1.4.9及以下版本中就会运行失败。假如你在项目中真的使用了低版本的Angular并且造成运行失败,我们还可以稍微改动一下模板,采用ng-include来实现同样的功能

rush:xhtml;">

{{currentFolder.name}}

我们在模板中去掉了folder-tree指令,使用了ng-include指令,再次将tree.html模板引入进来,需要注意的是,因为ng-include会创建一个独立的作用域,为了让其正确的引用到currentFolder变量,我们需要在每个ng-include中初始化currentFolder,将其赋值为ng-repeat中的当前subfolder,另外,别忘了ng-include指令中模板路径前后加上单引号。

这样确实可以,但是你可能觉得有些遗憾,没能使用最好的解决方案来实现这个树结构。

其实这个问题早就有人吐槽了,令人惊喜的是,有个叫Mark的伙计写了一个service专门解决这个问题:

rush:js;"> /* * An Angular service which helps with creating recursive directives. * @author Mark Lagendijk * @license MIT */ angular.module('RecursionHelper',[]).factory('RecursionHelper',['$compile',function($compile){ return { /** * Manually compiles the element,fixing the recursion loop. * @param element * @param [link] A post-link function,or an object with function(s) registered via pre and post properties. * @returns An object containing the linking functions. */ compile: function(element,link){ // normalize the link parameter // 如果link参数是对象类型link:{pre: function(...){},post: function(...){}}则不做处理 // 如果link参数是函数类型则将其作为post-link函数在$compile之后调用 if(angular.isFunction(link)){ link = { post: link }; }
  // Break the recursion loop by removing the contents 
  // <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>并清空当前元素的<a href="https://www.jb51.cc/tag/neirong/" target="_blank" class="keywords">内容</a>,后面进行编译 
  var contents = element.contents().remove(); 
  var compiledContents; 

  return { 
    pre: (link && link.pre) ? link.pre : null,/** 
     * Compiles and re-adds the contents 
     * 编译和重新<a href="https://www.jb51.cc/tag/tianjia/" target="_blank" class="keywords">添加</a><a href="https://www.jb51.cc/tag/neirong/" target="_blank" class="keywords">内容</a>到当前元素 
     */ 
    post: function(s<a href="https://www.jb51.cc/tag/cop/" target="_blank" class="keywords">cop</a>e,element){ 
      // Compile the contents 
      if(!compiledContents){ 
        compiledContents = $compile(contents); 
      } 
      // Re-add the compiled contents to the element 
      compiledContents(s<a href="https://www.jb51.cc/tag/cop/" target="_blank" class="keywords">cop</a>e,function(clone){ 
        element.append(clone); 
      }); 

      // Call the post-linking function,if any 
      if(link && link.post){ 
        link.post.apply(null,arguments); 
      } 
    } 
  }; 
} 

};
}]);

现在我们只需引入这个模块,然后在directive中使用RecursionHelper这个service,调用其compile手动编译指令对应的元素节点:

rush:js;"> angular.module('treeDemo',['RecursionHelper']) .controller("TreeController",function(RecursionHelper) { return { restrict: "E",templateUrl: 'template/tree.html',compile: function(element) { // 我们这里使用RecursionHelper的compile方法编译指令当前元素,这里第二个参数指定一个函数,相当于常用的link函数 // 当然我们也可以指定一个对象,里面包含pre和post函数,分别对应pre-link和post-link return RecursionHelper.compile(element,function(scope,iElement,iAttrs,controller,transcludeFn){ // Define your normal link function here. // Alternative: instead of passing a function,// you can also pass an object with // a 'pre'- and 'post'-link function.
    // 这里可以往s<a href="https://www.jb51.cc/tag/cop/" target="_blank" class="keywords">cop</a>e中绑定一些变量 
    s<a href="https://www.jb51.cc/tag/cop/" target="_blank" class="keywords">cop</a>e.variable = 'hello world'; 
  }); 
} 

};
});

在上面代码中,我们在创建treeDemo模块时引入RecursionHelper模块,然后在创建folderTree指令时使用RecursionHelper服务,并且在compile方法调用RecursionHelper的compile方法,即可修复上面的问题。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...