在 angular 项目中动态加载 bootstrap 4 手风琴

时间:2021-05-25 11:50:09

标签: javascript angular bootstrap-4

我正在为我的 angular 项目使用 bootstrap 4 手风琴。这是我的手风琴

   <div id="accordion" class="accordion">
                        <div class="card mb-0">
                            <div class="card-header collapsed" data-toggle="collapse" href="#collapseOne">
                                <a class="card-title float-none">
                                   Tank Id
                                </a>
                            </div>
                            <div id="collapseOne" class="collapse" data-parent="#accordion">
                              Tank Description
                            </div>
                            <div class="card-header collapsed" data-toggle="collapse" data-parent="#accordion"
                                href="#collapseTwo">
                                <a class="card-title float-none">
                                    Tank Id
                                </a>
                            </div>
                            <div id="collapseTwo" class="collapse" data-parent="#accordion">
                                Tank Description
                            </div>

                        </div>
                    </div>

目前这运行良好。但现在我需要根据下面的 json 数组加载手风琴标题和内容。

    {
"tanks":[
{
"id":"KT025",
"description":"kt025 is large tank with the 3 doors",
 
},
{
"id":"KT023",
"description":"kt023 is medium tank with the 2 doors",
},
{
"id":"KT034",
"description":"kt034 is small tank with the 2 doors",
}
]
}

我怎么做,如果你能帮上大忙?根据我的数据可以吗?

1 个答案:

答案 0 :(得分:0)

根据您的对象和给定的详细信息,您可以执行此操作。 详细可以参考ng-repeat指令here

<div ng-app='myApp' ng-controller="Main">
<div ng-repeat="n in object;">
  <div id="accordion" class="accordion" ng-repeat="k in n">
                        <div class="card mb-0">
                            <div class="card-header collapsed" data-toggle="collapse" href="#collapseOne">
                                <a class="card-title float-none">
                                   Tank Id {{k.id}}
                                </a>
                            </div>
                            <div id="collapseOne" class="collapse" data-parent="#accordion">
                              Tank Description {{k.description}}
                            </div>
                        </div>
                    </div>
</div>
</div>

JS代码

var myApp = angular.module('myApp', []);
function Main($scope){
  $scope.object =  {
                    "tanks":[
                            {
                            "id":"KT025",
                            "description":"kt025 is large tank with the 3 doors",
                             
                            },
                            {
                            "id":"KT023",
                            "description":"kt023 is medium tank with the 2 doors",
                            },
                            {
                            "id":"KT034",
                            "description":"kt034 is small tank with the 2 doors",
                            }
                            ]
                   }
};