我从ajax调用中获得了一系列常见问题解答以及一系列独立的常见问题解答类别,我尝试做某些事情,当您点击该常见问题解答的类别时,只有那些显示匹配的categoryId。我一直在尝试很多不同的东西,包括ng-show,但现在我的努力集中在试图获得|过滤:工作。任何帮助将不胜感激......以下是代码:
<div data-ng-controller="FAQController as dashboard" id="ctrler">
<div ng-repeat="cat in dashboard.categories" class="col-md-2">
<a data-ng-click="dashboard.filterForAnimation(cat)" href="#">
<div class="panel category-panel" data-fill-color="" data-context="">
<div class="panel-body background-6">
<span hidden class="cat-question-id" value="{{cat.id}}"></span>
<h4 class="question-category category-1">{{cat.name}}</h4>
</div>
</div>
</a>
</div>
<!-- visbile FAQs -------------------->
<div class="col-md-4 FAQ fades" data-ng-repeat="x in dashboard.visibleFaqs track by x.id | filter:dashboard.filter">
<div class="panel-body background-6 bg-grd-dark" data-fill-color="true">
<h4 class="headline question">{{x.question}} </h4>
<p class="answer">{{x.answer}}</p>
<a type="button" class="btn btn-success mb-1x mr-1x" href="/FAQs/{{x.id}}/edit">Edit</a>
<button type="button" data-ng-click="dashboard.delete(FAQ)" class="btn btn-danger mb-1x mr-1x" data="{{x.id}}">Delete</button>
</div><!-- /.panel-body -->
</div>
</div>
</div>
这是控制器:
(function () {
"use strict";
angular.module(APPNAME)
.controller("FAQController", FAQController);
FAQController.$inject = ['$scope', '$baseController', '$faqService'];
function FAQController($scope, $baseController, $faqService) {
var vm = this;
vm.headingInfo = "FAQs"
vm.faqItems = null;
vm.categories = null;
vm.visibleFaqs = null;
vm.selectedCategoryId = null;
vm.$faqService = $faqService;
vm.$scope = $scope;
vm.receiveFAQItems = _receiveFAQItems;
vm.onError = _onError;
vm.delete = _delete;
vm.deleteSuccess = _deleteSuccess;
vm.getItemSuccess = _getItemSuccess;
vm.filterForAnimation = _filterForAnimation;
vm.filter = _filter;
$baseController.merge(vm, $baseController);
vm.notify = vm.$faqService.getNotifier($scope);
render();
function render() {
vm.$faqService.getCategories(vm.getItemSuccess, vm.onError);
vm.$faqService.ajaxGetAll(vm.receiveFAQItems, vm.onError);
}
function _getItemSuccess(data) {
vm.notify(function () {
vm.categories = data.items;
});
}
function _receiveFAQItems(data) {
vm.notify(function () {
console.log(data.items);
vm.faqItems = data.items;
})
}
function _onError(jqXhr, error) {
console.error("error");
}
function _delete(faqArray) {
var index = vm.faqItems.indexOf(faqArray);
vm.faqItems.splice(index, 1);
var id = faqArray.id;
vm.$faqService.ajaxDelete(id, vm.deleteSuccess, vm.onError)
}
function _deleteSuccess(faqArray) {
console.log("delete successful");
}
function _filterForAnimation(item) {
console.log("category clicked");
vm.selectedCategoryId = item.id;
for (var i = 0; i < vm.faqItems.length; i++) {
if (vm.faqItems[i].categoryId == vm.selectedCategoryId){
vm.filter(vm.faqItems[i].categoryId, vm.selectedCategoryId)
}
}
}
function _filter(faq, categoryId) {
if (faq == categoryId) {
return true;
}
else {
return false;
}
}
}
})()
答案 0 :(得分:0)
与之前的答案相同,但方法不同:
将_filter
功能更改为:
function _filter(faq) {
return faq.categoryId == vm.selectedCategoryId;
}
vm.selectedCategoryId
动态设置_filterForAnimation
,因此可以使用。
你也可以像这样使用角度过滤器提供程序:
function _filter() {
return function(items, categoryId) {
return items.filter(function(item) {
return item.id == categoryId;
});
}
声明它
angular.module(APP_NAME)
//...
.filter('categoryFilter', _filter);
在你的标记中:
data-ng-repeat="x in dashboard.visibleFaqs track by x.id | categoryFilter:dashboard.selectedCategoryId"