AngularJs,在手风琴中将它们的标题分别列出

时间:2014-04-11 04:49:11

标签: javascript angularjs accordion

我是angularJs的新手,希望在angularJs中重写一个应用程序,但我坚持这个:

我有一个带有以下标记的自定义手风琴:

<div class="accord_main_wrap" ng-controller="catController">
    <div class="accord_item_wrap" ng-repeat="head in heads">
        <p class="accord_head" data-notvisible="true">{{head.text}}</p>
        <div class="accord_content_wrap">
            <ul>
                <li ng-repeat="sub in subs">{{sub.text}}</li> 
            </ul>
        </div>
    </div>
</div>

控制器:

function catController($scope, $http) {
    $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
    $scope.heads = [];
    $scope.subs = [];
    $http.post(apiURL,$.param({maincat:"true"}))
    .success(function(mainCatData,status,headers,config){
        for(var d=0;d<mainCatData.cat.length;d++){
            $scope.heads.push(mainCatData.cat[d]);
        }
        for(var h=0;h<$scope.heads.length;h++){
            $http.post(apiURL,$.param({subcat:$scope.heads[h].id}))
                .success(function(subCatdata,stat,hea,conf){
                        for(var s=0;s<subCatdata.cat.length;s++){
                            $scope.subs.push(subCatdata.cat[s]);
                        }
                });
        }
    });

问题:

很明显,每个手风琴标题应该有自己的子标题。 但正如您在上面的代码中所看到的, $scope.subs 会将所有子标题混合并分配给所有手风琴。

那我怎样才能将正义的子标题分配给各自的主题?

1 个答案:

答案 0 :(得分:1)

$scope.heads中的每个项目都应该有一个名为subs ...

的子集合
function catController($scope, $http) {
    $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
    $scope.heads = [];
    $http.post(apiURL,$.param({maincat:"true"}))
    .success(function (mainCatData) {
        for (var d = 0; d < mainCatData.cat.length; d++) {
            var head = mainCatData.cat[d];
            $scope.heads.push(head);
            getSubs(head);
        }
    });

    function getSubs(head) {
        head.subs = [];
        $http.post(apiURL, $.param({subcat: head.id}))
            .success(function (subCatdata) {
                    for (var s = 0; s < subCatdata.cat.length; s++) {
                        head.subs.push(subCatdata.cat[s]);
                    }
            });
    }
}