我是 JS 和 Angular 的新手。我在从 JSON 文件获取数据时遇到问题。该结构如下所示:

我的服务如下所示:

(function(){ 
angular.module('models') 
.service('lessonService', function ($http, Backand) { 
 
    var baseUrl = '/1/objects/'; 
    var objectName = ['lessons/', 'subCategories', 'lessons_categories_switch']; 
    function getUrlAllLessons () { 
        return Backand.getApiUrl() + baseUrl + objectName[0]; 
    }; 
    /*All Lessons*/ 
    getLessons = function () { 
        return $http.get(getUrlAllLessons()); 
    }; 
    /*Subcategories*/ 
    function getUrlSubcategories () { 
        return Backand.getApiUrl() + baseUrl + objectName[1]; 
    }; 
    getCategories = function () { 
        return $http.get(getUrlSubcategories()); 
    }; 
    /*Category Lesson Swith*/ 
    function getUrlSwitch () { 
        return Backand.getApiUrl() + baseUrl + objectName[2]; 
    }; 
    getSwitch = function () { 
        return $http.get(getUrlSwitch()); 
    }; 
    return { 
        getLessons: getLessons, 
        getCategories: getCategories, 
        getSwitch: getSwitch 
    }; 
}) 
 
})(); 

我的 Controller 如下:

    (function(){ 
    angular.module('appLessons', ['backand'])    
    .controller('lessonCtrl', function($scope, Backand, lessonService){ 
        $scope.lessons = []; 
        $scope.subCategories = []; 
        $scope.switches = [];    
        function getAllLessons() { 
            lessonService.getLessons() 
                .then(function (result) { 
                $scope.lessons = result.data.data; 
            }); 
        }; 
        function getAllSubCategories() { 
            lessonService.getCategories() 
                .then(function (result) { 
                $scope.subCategories = result.data.data; 
            }); 
        }; 
        function getAllSwitch() { 
            lessonService.getSwitch() 
                .then(function (result) { 
                $scope.switches = result.data.data; 
            }); 
        };     
        getAllLessons(); 
        getAllSubCategories(); 
        getAllSwitch(); 
    })     
    .controller('lessonDetailCtrl', ['$scope', '$routeParams', '$http', 'Backand', function($scope, $routeParams, $http, Backand) { 
        $http.get(Backand.getApiUrl() + '/1/objects/lessons/' + $routeParams.id) 
        .success(function(data) { 
            $scope.lesson = data; 
            $scope.code = $scope.lesson.youtubeVideoCode; 
        }); 
        $scope.closeLeftBar = function (){ 
            var categoryList = $('.left-lessons'); 
            categoryList.hide(); 
        };     
    }]) 
})(); 

我正在尝试获取属于其中一个子类别的所有类(class)的名称。

在 View 中我有以下代码:

<div ng-repeat="category in subCategories"> 
    <div ng-show="category" class="col-md-3 lesson-tile"> 
        <h2>{{ category.name }} </h2> // this works 
        <div ng-repeat="lesson in category.lessons"> 
            <div ng-show="lesson" class="col-md-3 lesson-tile"> 
                <h2>{{ lesson.name }} </h2> // I have problems here 
            </div> 
        </div> 
    </div> 
</div> 

我做错了什么?感谢您的帮助,谢谢大家。

请您参考如下方法:

您的类别内没有lessons Prop 。您应该包含或检查所有类(class)并按类别显示。

你能试试这个吗:

<div ng-repeat="category in subCategories"> 
    <div ng-show="category" class="col-md-3 lesson-tile"> 
        <h2>{{ category.name }} </h2> // this works 
        <div ng-repeat="lesson in lessons"> 
            <div ng-show="checkCategory(category,lesson)" class="col-md-3 lesson-tile"> 
                <h2>{{ lesson.name }} </h2> // I have problems here 
            </div> 
        </div> 
    </div> 
</div> 

吴函数:

$scope.checkCategory = function(currentCategory,currentLesson){ 
    console.log(currentCategory); 
    console.log(currentLesson); 
    console.log($scope.switches); 
    if($filter('filter')($scope.switches, {category : currentCategory.id + "", lesson: currentLesson.id + ""}).length > 0 ) 
        return true;  
    return false; 
}; 

Ps:过滤器可以改变你的json对象。也许你应该像这样过滤类别:

{Id:currentCategory.Id}

https://docs.angularjs.org/api/ng/filter/filter


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!