Recursion in Angular directives

前端 未结 9 1685
不思量自难忘°
不思量自难忘° 2020-11-22 04:24

There are a couple of popular recursive angular directive Q&A\'s out there, which all come down to one of the following solutions:

  • manually incrementally \
9条回答
  •  天命终不由人
    2020-11-22 04:31

    As of Angular 1.5.x, no more tricks are required, the following has been made possible. No more need for dirty work arounds!

    This discovery was a by product of my hunt for a better/cleaner solution for a recursive directive. You can find it here https://jsfiddle.net/cattails27/5j5au76c/. It supports as far is 1.3.x.

    angular.element(document).ready(function() {
      angular.module('mainApp', [])
        .controller('mainCtrl', mainCtrl)
        .directive('recurv', recurveDirective);
    
      angular.bootstrap(document, ['mainApp']);
    
      function recurveDirective() {
        return {
          template: '
    • {{t.sub}}
    ', scope: { tree: '=' }, } } }); function mainCtrl() { this.tree = [{ title: '1', sub: 'coffee', children: [{ title: '2.1', sub: 'mocha' }, { title: '2.2', sub: 'latte', children: [{ title: '2.2.1', sub: 'iced latte' }] }, { title: '2.3', sub: 'expresso' }, ] }, { title: '2', sub: 'milk' }, { title: '3', sub: 'tea', children: [{ title: '3.1', sub: 'green tea', children: [{ title: '3.1.1', sub: 'green coffee', children: [{ title: '3.1.1.1', sub: 'green milk', children: [{ title: '3.1.1.1.1', sub: 'black tea' }] }] }] }] }]; }
    
    

提交回复
热议问题