Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在ng repeat中通过深度对象循环_Javascript_Angularjs - Fatal编程技术网

Javascript 在ng repeat中通过深度对象循环

Javascript 在ng repeat中通过深度对象循环,javascript,angularjs,Javascript,Angularjs,我是有角度的,我有一个像这样的物体 var items = [{ title: 'Something', children: [ { title: 'Hello World' }, { title: 'Hello Overflow' }, { title: 'John Doe', children: [ { title: 'Amazing title' }, { title: 'Goo

我是有角度的,我有一个像这样的物体

var items = [{
    title: 'Something',
    children: [
        { title: 'Hello World' },
        { title: 'Hello Overflow' },
        { title: 'John Doe', children: [
            { title: 'Amazing title' },
            { title: 'Google it' },
            { title: 'I'm a child', children: [
                { title: 'Another ' },
                { title: 'He\'s my brother' },
                { title: 'She\'s my mother.', children: [
                    {title: 'You never know if I'm going to have children'}
                ]}
            ]}
        ]}
    ]
}];
我不想重复所有这些,所以我有这样的东西

var items = [{
    title: 'Something',
    children: [
        { title: 'Hello World' },
        { title: 'Hello Overflow' },
        { title: 'John Doe', children: [
            { title: 'Amazing title' },
            { title: 'Google it' },
            { title: 'I'm a child', children: [
                { title: 'Another ' },
                { title: 'He\'s my brother' },
                { title: 'She\'s my mother.', children: [
                    {title: 'You never know if I'm going to have children'}
                ]}
            ]}
        ]}
    ]
}];
•一些

•你好,世界

•你好溢出

•无名氏

•惊人的标题

•谷歌it

•我是个孩子

•另一个

•他是我的兄弟

•她是我的母亲

•你永远不知道我是否会有孩子

问题是我不知道这个物体会有多深,或者里面有什么。所以我不能手动操作。我已经在底部提供的小提琴中使用
ng repeat
完成了一个基本循环,但我不知道如何自动循环这些循环并创建嵌套的
  • 实现这一目标的最佳方式是什么


    演示:

    您可能需要创建自己的指令来传递要迭代的对象。对传入的对象进行监视,并在该对象激发时运行一些递归函数,将元素附加到指令所在的元素


    您可以从directive link函数的element参数获取DOM元素。显然,可以使用相同的元素参数附加DOM元素

    您不需要定制指令,您需要的是使用一个内联模板来调用它的self

    我用叉子叉了你的小提琴

    基本上是这样的:

    <script type='text/ng-template' id="item.html">
        ...
        <div ng-repeat="x in x.childrens" ng-include="'item.html'"></div>
    </script>
    ...
    <div ng-repeat="x in things" ng-include="'item.html'"></div>
    
    
    ...
    ...
    
    我应该注意,您实际上并没有覆盖
    x
    ,因为angular为每个重复项创建了一个新范围。

    给您:

    html

    <div ng-app="app" ng-controller="test">
       <ul>
           <li nested-item ng-repeat="item in items">{{item.title}}</li>
       </ul>         
    </div>
    
    <div ng-app="app" ng-controller="test">
       <ul template-code>
           <li nested-item ng-repeat="item in items">{{item.title}}</li>
       </ul>         
    </div>
    
    JavaScript

    var items = [{
        title: 'Something',
        children: [
            { title: 'Hello World' },
            { title: 'Hello Overflow' },
            { title: 'John Doe', children: [
                { title: 'Amazing title' },
                { title: 'Google it' },
                { title: 'Im a child', children: [
                    { title: 'Another ' },
                    { title: 'He\'s my brother' },
                    { title: 'She\'s my mother.', children: [
                        {title: 'You never know if im going to have children'}
                    ]}
                ]}
            ]}
        ]
    }];
    
    var app = angular.module('app', []);
    
    app.controller('test', function( $scope ) {
        $scope.items = items;
    });
    
    
    app.directive('nestedItem', ['$compile', function($compile){
        return {
            restrict: 'A',
            link: function(scope, element){
            console.log(element);
                if (scope.item.children){
                    var html = $compile('<ul><li nested-item ng-repeat="item in item.children">{{item.title}}</li></ul>')(scope);
                    element.append(html);
                }
            }
        };
    }]);
    
    var items = [{
        title: 'Something',
        children: [
            { title: 'Hello World' },
            { title: 'Hello Overflow' },
            { title: 'John Doe', children: [
                { title: 'Amazing title' },
                { title: 'Google it' },
                { title: 'Im a child', children: [
                    { title: 'Another ' },
                    { title: 'He\'s my brother' },
                    { title: 'She\'s my mother.', children: [
                        {title: 'You never know if im going to have children'}
                    ]}
                ]}
            ]}
        ]
    }];
    
    var app = angular.module('app', []);
    
    app.controller('test', function( $scope ) {
        $scope.items = items;
    });
    
    app.directive('templateCode', function(){
        return {
            restrict: 'A',
            controller: function(){},
            compile: function(element){
                element.removeAttr('template-code');
                //ATTENTION: We need to trim() here. Otherwise AngularJS raises an exception
                //later when we want to use the templateCode in a $compile function. 
                //Be aware that we assume a modern browser 
                //that already ships with a trim function.
                //It's easy to secure that with a polyfill.
                var templateCode = element.parent().html().trim();
                return function(scope, iElement, iAttrs, controller){
                    controller.templateCode = templateCode;
                }
            }
        }
    });
    
    app.directive('nestedItem', ['$compile', function($compile){
        return {
            restrict: 'A',
            require: '^templateCode',
            link: function(scope, element, iAttr, controller){ 
                if (scope.item.children){
                    scope.items = scope.item.children;         
                    var html = $compile(controller.templateCode)(scope);
                    element.append(html);
                }
            }
        };
    }]);
    

    普朗克:谢谢你的回答,太好了,我正在考虑哪种方法更好。很难说,但我认为这种方法可能比场景背后的另一种方法做的工作要少得多。这种方法唯一让我感到不舒服的地方是html的重复(指令中的内联html)。然而,我认为这是可以做的。我以后可能会发布更新。谢谢你的帮助,这是一个很好的答案。但我更喜欢另一种方法+1