Javascript AngularJS在循环内动态创建指令 问题陈述

Javascript AngularJS在循环内动态创建指令 问题陈述,javascript,jquery,angularjs,angularjs-directive,angularjs-ng-repeat,Javascript,Jquery,Angularjs,Angularjs Directive,Angularjs Ng Repeat,下面有两个类似这样的父数据结构和子数据结构,其中在父div头上单击dynamic指令,以显示创建的相应子记录 但当我点击父标题链接时,相应的子项并没有显示在表结构中。有人能看出哪里错了吗 <script> var app = angular.module('TestApp', []); app.controller('TestCtrl', function ($scope) { $scope.parents = [ { pare

下面有两个类似这样的父数据结构和子数据结构,其中在父div头上单击dynamic指令,以显示创建的相应子记录

但当我点击父标题链接时,相应的子项并没有显示在表结构中。有人能看出哪里错了吗

<script>
    var app = angular.module('TestApp', []);
    app.controller('TestCtrl', function ($scope) {
        $scope.parents = [
            { parent_id: '1', name: 'Parent 1', count: '2' },
            { parent_id: '2', name: 'Parent 2', count: '3' },
            { parent_id: '3', name: 'Parent 3', count: '1' }
        ];

        $scope.getchild= function (parent) {
            var children_list = [
            { id: '1', parent_id: '1', name: 'Test Child 1' },
            { id: '2', parent_id: '1', name: 'Test Child 2' },
            { id: '3', parent_id: '2', name: 'Test Child 3' },
            { id: '4', parent_id: '2', name: 'Test Child 4' },
            { id: '5', parent_id: '2', name: 'Test Child 5' },
            { id: '6', parent_id: '3', name: 'Test Child 6' }
            ];

            var directive_name = 'clschild_' + parent.parent_id;
            app.directive(directive_name, function() {

                var child_html = '<div class="panel-body"> \
                    <table ng-attr-id="tblchild_'+ parent.parent_id + '" class="table table-hover"> \
                        <thead> \
                            <tr> \
                                <td>Name</td> \
                            </tr> \
                        </thead> \
                        <tbody> \
                            <tr ng-repeat="child in children"> \
                                <td>{{child.name}}</td> \
                            </tr> \
                        </tbody> \
                    </table> \
                </div>';
                return {
                    template: child_html,
                    link: function (scope) {
                        scope.$apply(function () { scope.children = children_list; });
                    }
                }
            });
        };
    });

</script>

var app=angular.module('TestApp',[]);
app.controller('TestCtrl',函数($scope){
$scope.parents=[
{parent_id:'1',name:'parent 1',count:'2'},
{parent_id:'2',name:'parent 2',count:'3'},
{parent_id:'3',name:'parent 3',count:'1'}
];
$scope.getchild=函数(父级){
变量子项列表=[
{id:'1',parent_id:'1',name:'Test Child 1'},
{id:'2',父\u id:'1',名称:'Test Child 2'},
{id:'3',父\父id:'2',名:'Test Child 3'},
{id:'4',父\u id:'2',名称:'Test Child 4'},
{id:'5',父\u id:'2',名称:'Test Child 5'},
{id:'6',父\u id:'3',名称:'Test Child 6'}
];
var指令_name='clschild_'+parent.parent_id;
应用指令(指令名称,函数(){
var child_html=\
\
\
\
名字\
\
\
\
\
{{child.name}\
\
\
\
';
返回{
模板:child_html,
链接:功能(范围){
scope.$apply(函数(){scope.children=children\u list;});
}
}
});
};
});
相应的HTML是

<div ng-app="TestApp" ng-controller="TestCtrl">
    <div class="panel panel-default" ng-repeat="parent in parents">
        <div class="panel-heading clearfix">
            <a data-toggle="collapse" ng-href="#divchild_{{parent.id}}" ng-click="getchild(parent);" class="pull-left" style="padding-top: 7.5px;">{{parent.name}}</a>
        </div>
        <div ng-attr-id="divchild_{{parent.id}}" class="panel-collapse collapse">
            <div class="clschild_{{parent.id}}">
                <div class="panel-body">
                    aaa
                </div>
            </div>
        </div>
    </div>
</div>

aaa
这里有一个例子

我正在一天一天地学习angularjs,所以如果我在实现上述内容时出错,请容忍我


非常感谢您的帮助。谢谢。

我希望这个例子对您有所帮助

如果您想创建动态指令,您必须使用它们的所有属性,如scope、template、ngTransclude、replace和

此示例演示如何使用作用域将数据从控制器绑定到一个指令

angular.module(“app”,[]);
角度.module(“app”).controller(“ctrl”),函数($scope,$filter){
$scope.parents=[
{id:1,名称:“测试父级1”,计数:“2”},
{id:2,名称:“测试父级2”,计数:“3”},
{id:3,名称:“测试父级3”,计数:“1”}
];
$scope.children=[
{id:1,父\u id:1,名称:“测试子1”},
{id:1,父\u id:1,名称:“测试子2”},
{id:1,父\u id:2,名称:“测试子3”},
{id:1,父\u id:2,名称:“测试子4”},
{id:1,父\u id:2,名称:“测试子5”},
{id:1,父\u id:3,名称:“测试子6”}
];
$scope.getChild=函数(parentId){
var findChildren=$filter(“filter”)($scope.children{
父id:parentId
});
$scope.setChild=findChildren;
}
});
角度。模块(“app”)。指令(“指令”,函数(){
var template=“”+
"" +
"" +
"" +
“姓名”+
"" +
"" +
"" +
"" +
“{{child.name}}”+
"" +
"" +
"" +
"";
返回{
范围:{
孩子:“=”
},
模板:模板,
链接:函数(范围、elm、属性、ctrl){
作用域:$watch(“子”,函数(newValue){
如果(新值)
scope.children=newValue;
},对);
}
};
});

父列表
  • {{parent.name} 单击以获取子对象

子列表
这是您的问题的另一个测试用例,请看一看,希望您能得到一些有用的东西



{{parent.id}
面板内容-{child.parent_id}

我认为更好的方法是,创建一个指令,并使用
ng repeat
来显示所有数据。您不能在ControllerHanks中创建指令,因此您指的是针对父级和子级的两个单独指令。我认为您显示子级的方式是错误的!你必须改变它
var app = angular.module('TestApp', []);
app.controller('TestCtrl', ['$scope', function ($scope) {
    $scope.clicked_parent = 0;
    $scope.parents = [
        { id: '1', name: 'Test Parent 1', count: '2' },
        { id: '2', name: 'Test Parent 2', count: '3' },
        { id: '3', name: 'Test Parent 3', count: '1' }
    ];

    $scope.children_list = [
      { id: '1', parent_id: '1', name: 'Test Child 1' },
      { id: '2', parent_id: '1', name: 'Test Child 2' },
      { id: '3', parent_id: '2', name: 'Test Child 3' },
      { id: '4', parent_id: '2', name: 'Test Child 4' },
      { id: '5', parent_id: '2', name: 'Test Child 5' },
      { id: '6', parent_id: '3', name: 'Test Child 6' }
    ];

    $scope.isParentTagClicked = false;
    $scope.set_parent_id = function(_id){
      $scope.isParentTagClicked = !$scope.isParentTagClicked;
      if ($scope.isParentTagClicked){
        $scope.clicked_parent = _id; 
      }else{
        $scope.clicked_parent     = 0;
      }
    };

}]);
<!DOCTYPE html>
<html ng-app="TestApp">

  <head>
    <script src="//code.angularjs.org/1.2.20/angular.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <script src="script.js"></script>
  </head>

  <body>
    <div class="container" ng-controller="TestCtrl">

      <div class="panel panel-default" ng-repeat="parent in parents">
        <div class="panel-heading" ng-click="set_parent_id(parent.id)">{{parent.id}}</div>
        <div class="panel-body" ng-repeat="child in children_list" ng-show="child.parent_id == clicked_parent" ng-if="child.parent_id == parent.id">
          Panel content - {{child.parent_id}}
        </div>
      </div>

    </div>
  </body>

</html>