关于AngularJs数据递归呈现的实现的几种方式

r囧r小猫 2022-09-26 02:43 277阅读 0赞

在实践中,常常会有数据结构一致但深度不确定的数据。而通常情况下,对数据的处理会用到递归的方式。例如网站的无限级分类的展现,分类数据结构:

复制

  1. var cate = [
  2. {
  3. cateId: 1,
  4. cateName: '前端技术',
  5. child: [
  6. {
  7. cateId: 4,
  8. cateName: 'JavaScript'
  9. },
  10. ...
  11. ]
  12. },
  13. {
  14. cate: 2,
  15. cateName: '后端技术',
  16. child:[
  17. {
  18. cateId: 3,
  19. cateName: 'PHP',
  20. child: [
  21. {
  22. cateId: 6,
  23. cateName: 'ThinkPHP'
  24. },
  25. ...
  26. ]
  27. }
  28. ]
  29. }
  30. ];

用jq常常通过如下方式实现,示例代码如下:

复制

  1. /**
  2. * * 解析模板文件
  3. * @param template 模板字符串
  4. * @param scope 模板作用域
  5. * return [string] 解析过后的字符串
  6. */
  7. function templateParse(template, scope) {
  8. if(typeof template != "string") return ;
  9. return template.replace(/\{\w+\}/gi, function(matchs) {
  10. var returns = scope[matchs.replace(/(\{)|(\})/g, "")];
  11. return (returns + "") == "undefined"? "": returns;
  12. });
  13. }
  14. /**
  15. * 解析并插入分类
  16. */
  17. $('#category').append(function(){
  18. var template = '<a href="{cateId}.html">{cateName}</a>';
  19. var ret = (function(c){
  20. if(!c || !c.length) return '';
  21. var ret = '<ul>';
  22. for(var i = 0, j = c.length; i < j; i++){
  23. ret += '<li>';
  24. ret += templateParse(template, c[i]);
  25. ret += arguments.callee(c[i].child);
  26. ret += '</li>';
  27. }
  28. return (ret + '</ul>');
  29. })(cate);
  30. return ret;
  31. });

以上页面可以点击”jq实现无限级分类展示演示DEMO“,查看对应结果。

angularJs中基于模板递归的实现

同样的原理,可以通过组合angularJs内置的ng-includeng-init来达到递归的效果,示例模板如下:

复制

  1. <script id="recursion" type="text/ng-template">
  2. <li ng-repeat="item in cate">
  3. <a href="{ {item.cateId}}">{ { item.cateName}}</a>
  4. <ul ng-if="item.child.length" ng-include="'recursion'" ng-init="cate=item.child"></ul>
  5. </li>
  6. </script>

调用方式如下:

复制

  1. <div ng-app="demo">
  2. <div ng-controller="demo">
  3. <ul ng-include="'recursion'"></ul>
  4. </div>
  5. </div>

实现效果演示DEMO,”AngularJ基于模板递归实现分类展示“

基于指令递归的实现

同样的道理,在指令中,咱们可以这么来干(内容参考自angular-recursion):

复制

  1. angular.module('demo').directive('recursion',function($compile){
  2. function cpl(element, link){
  3. // Normalize the link parameter
  4. if(angular.isFunction(link)){
  5. link = { post: link };
  6. }
  7. // Break the recursion loop by removing the contents
  8. var contents = element.contents().remove();
  9. var compiledContents;
  10. return {
  11. pre: (link && link.pre) ? link.pre : null,
  12. /**
  13. * Compiles and re-adds the contents
  14. */
  15. post: function(scope, element){
  16. // Compile the contents
  17. if(!compiledContents){
  18. compiledContents = $compile(contents);
  19. }
  20. // Re-add the compiled contents to the element
  21. compiledContents(scope, function(clone){
  22. element.append(clone);
  23. });
  24. // Call the post-linking function, if any
  25. if(link && link.post){
  26. link.post.apply(null, arguments);
  27. }
  28. }
  29. };
  30. }
  31. return {
  32. restrict:'A',
  33. scope: { recursion:'='},
  34. template: '<li ng-repeat="item in recursion">\
  35. <a href="{ {item.cateId}}.html">{ {item.cateName}}</a>\
  36. <ul recursion="item.child">\
  37. </ul>\
  38. </li>',
  39. compile: function(element){
  40. return cpl(element, function(scope, iElement, iAttrs, controller, transcludeFn){
  41. // Define your normal link function here.
  42. // Alternative: instead of passing a function,
  43. // you can also pass an object with
  44. // a 'pre'- and 'post'-link function.
  45. });
  46. }
  47. };
  48. });

关于recursion指令

以上两种方式实现与模板耦合的过于紧密,有没有办法可以像如下的方式来使用呢?(未完,等续)

复制

  1. <ul recursion="cate">
  2. <li ng-repeat="item in cate">
  3. <a href="{ {item.cateId}}">{ {item.cateName}}</a>
  4. <ul recursion-child='item.child'></ul>
  5. </li>
  6. </ul>

源引:http://www.lyblog.net/detail/596.html

发表评论

表情:
评论列表 (有 0 条评论,277人围观)

还没有评论,来说两句吧...

相关阅读

    相关 实现方式

    递归函数最基本的特点是函数自身调用自身,但必须在调用自身前有条件判断,否则无限无限调用下去。 利用引用做参数   先不管引用做不做参数,必须先明白引用到底是什么?引用不过是