Javascript Angular js:ng repeat筛选器返回未定义的
我想要实现的是一个字母过滤器。从A到Z的一个按钮,用于过滤数据。单击字母A按钮时,应显示所有带有字母A的数据,其余数据将隐藏 这是我迄今为止尝试过的,但问题在于它返回的Javascript Angular js:ng repeat筛选器返回未定义的,javascript,angularjs,angularjs-ng-repeat,Javascript,Angularjs,Angularjs Ng Repeat,我想要实现的是一个字母过滤器。从A到Z的一个按钮,用于过滤数据。单击字母A按钮时,应显示所有带有字母A的数据,其余数据将隐藏 这是我迄今为止尝试过的,但问题在于它返回的undefined过滤器中的ng repeat。每次单击字母表按钮时,$scope.filterActive的参数都会返回下面的屏幕截图,但$scope.active会返回单击的字母表 角度: $scope.alphabet = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y
undefined
过滤器中的ng repeat
。每次单击字母表按钮时,$scope.filterActive
的参数都会返回下面的屏幕截图,但$scope.active
会返回单击的字母表
角度:
$scope.alphabet = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z".split(",");
$scope.active = null;
$scope.setActiveLetter = function(letter) {
$scope.active = letter.toLowerCase();
$scope.selectedLetter = letter.toLowerCase();
};
$scope.filterActive = function(value) {
if ($scope.active) {
// return value.charAt(0).toLowerCase() === $scope.active;
}
return true;
}
视图:
当您想要应用过滤器时,您需要确保数组变量中的数据可用 请检查以下示例代码
//make sure data is available or not
$scope.manufacturers = [];
$scope.filterActive = function(value) {
if ($scope.active) {
// return value.charAt(0).toLowerCase() === $scope.active;
}
return true;
}
//for example your calling API
function getdata(){
//response
$scope.manufacturers = response.length ? response : [];
}
getdata();
请检查jsfiddle to
希望这对你有帮助 如果你取下过滤器,它能工作吗?@ProfessorAllman你的意思是
ng repeat=“制造商中的数据| filterActive
?尝试这样做会导致过滤器错误我的意思是没有过滤器,以确保数据中有信息ng repeat=“制造商中的数据”“
。看起来代码应该可以正常工作。这可能是JSON问题吗?当我检查你的小提琴时,它真的很好用@ProfessorAllman@ProfessorAllman我已经在我的问题上添加了我的JSON数据。尝试了这个方法,但是当我单击我的按钮时,它仍然无法工作,并收到一个错误{id:undefined,name:undefined,documents:undefined,$$hashKey:“object:32”}
。在您的plunker中,您的筛选函数filterData
返回anyNameStartsWith
的返回值,它是真实的,而不是obj。抱歉,我的意思是您需要确保数组对象存在或不存在not@SantoshShinde是的,你能查查我的问题吗。我已经用我的样本数据更新了它。这可能是JSON问题吗?是的,我在获取数据之前看到了声明数组对象
1:{id: "53", name: "TEST 0", documents: Array(5), $$hashKey: "object:34"}
2:{id: "69", name: "Test 1", documents: Array(7), $$hashKey: "object:36"}
3:{id: "46", name: "Test 2", documents: Array(45), $$hashKey: "object:38"}
4:{id:"70", name: "Test 3", documents: Array(2), $$hashKey: "object:40"}
//make sure data is available or not
$scope.manufacturers = [];
$scope.filterActive = function(value) {
if ($scope.active) {
// return value.charAt(0).toLowerCase() === $scope.active;
}
return true;
}
//for example your calling API
function getdata(){
//response
$scope.manufacturers = response.length ? response : [];
}
getdata();