无法让 Angular 自定义过滤器工作
Cant get Angular custom filter to work
所以目的是根据它们的"四分之一"显示特定的单词。$scope.myFunc 有问题,因为当我尝试选择一个复选框而不是显示腐蚀性单词时,我得到"未定义不是一个函数"。有谁知道它可能是什么?
var app = angular.module("sight", []);
app.controller('WordController', function($scope){
$scope.myFunc = function(a) {
for(catagory in $scope.catagoryArray){
var c = $scope.catagoryArray[catagory];
if(c.on && a.quarter.indexOf(c.area) > -1){
return true;
}
}
};
$scope.catagoryArray = [{ area: 'colors', on: false}, { area: "numbers", on: false}, { area: "first", on: false}, { area: "second", on: false}, { area: "third", on: false},{ area: "fourth", on: false}];
$scope.words = [
{
name: 'Red',
sound: "audio/test.mp3",
quarter: ['colors']
},
{
name: 'White',
sound: "audio/test.mp3",
quarter: 'colors'
},
{
name: 'Blue',
sound: "audio/test.mp3",
quarter: 'colors'
},
{
name: 'Yellow',
sound: "audio/test.mp3",
quarter: 'colors'
},
{
name: 'Black',
sound: "audio/test.mp3",
quarter: 'colors'
},
{
name: 'Green',
sound: "audio/test.mp3",
quarter: 'colors'
},
{
name: 'One',
sound: "audio/test.mp3",
quarter: 'numbers'
},
{
name: 'Two',
sound: "audio/test.mp3",
quarter: 'numbers'
},
{
name: 'Three',
sound: "audio/test.mp3",
quarter: 'numbers'
},
{
name: 'Four',
sound: "audio/test.mp3",
quarter: 'numbers'
},
{
name: 'Five',
sound: "audio/test.mp3",
quarter: 'numbers'
},
{
name: 'Six',
sound: "audio/test.mp3",
quarter: 'numbers'
},
{
name: 'Seven',
sound: "audio/test.mp3",
quarter: 'numbers'
},
{
name: 'Eight',
sound: "audio/test.mp3",
quarter: 'numbers'
},
{
name: 'Nine',
sound: "audio/test.mp3",
quarter: 'numbers'
},
{
name: 'Ten',
sound: "audio/test.mp3",
quarter: 'numbers'
},
{
name: 'am',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'and',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'are',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'at',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'can',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'do',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'for',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'go',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'have',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'he',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'here',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'I',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'is',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'it',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'like',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'look',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'me',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'my',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'no',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'play',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'said',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'see',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'she',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'the',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'to',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'up',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'we',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'will',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'you',
sound: "audio/test.mp3",
quarter: 'one'
},
{
name: 'secondtest',
sound: "audio/test.mp3",
quarter: 2
},
{
name: 'thirdtest',
sound: "audio/test.mp3",
quarter: 'third'
},
{
name: 'will',
sound: "audio/test.mp3",
quarter: 'fourth'
}];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="sight">
<h4>If you don't want to practice all of the words, please choose the quarter or types you want to practice. You can choose more than one.</h4>
<div ng-controller="WordController">
<li ng-repeat="area in catagoryArray">
<label>
<input type="checkbox" ng-model="area.on">{{area.area}}
</label>{{area.on}}
</li>
<h1>Words</h1>
<ul>
<li ng-repeat="word in words | filter:myFunc">{{word.name}}</li>
</ul>
</div>
</body>
你的代码中有一个错误,你正在执行 a.quarter.indexOf 偶数,
{
name: 'secondtest',
sound: "audio/test.mp3",
quarter: 2
}
请查看此 Plunker 链接 http://plnkr.co/edit/CniKgrUirgrrryjCrlsq?p=preview
$scope.myFunc = function(a) {
for(catagory in $scope.catagoryArray){
var c = $scope.catagoryArray[catagory];
if(c.on && a.quarter instanceof Array &&
a.quarter.indexOf(c.area) > -1) {
return true;
}
if(c.on && a.quarter == c.area) {
return true;
}
}
return false;
};
您的问题在此行:
if(c.on && a.quarter.indexOf(c.area) > -1){
碰巧的是,在您的列表对象中,有一些对象具有quarter
numeric value
属性,indexOf
是一个存在于字符串和数组上的函数,但不存在于数字或任何其他类型的数据中。因此,如果您打算在字符串中搜索,只需Stringify
它。
if(c.on && String(a.quarter).indexOf(c.area) > -1){
或
var list = [].concat(a.quarter); //Search in an array
if (c.on && list.indexOf(c.area) > -1) {
return true;
}
var app = angular.module("sight", []);
app.controller('WordController', function($scope) {
$scope.myFunc = function(a) {
for (catagory in $scope.catagoryArray) {
var c = $scope.catagoryArray[catagory];
var list = [].concat(a.quarter);
if (c.on && list.indexOf(c.area) > -1) {
return true;
}
}
};
$scope.catagoryArray = [{
area: 'colors',
on: false
}, {
area: "numbers",
on: false
}, {
area: "first",
on: false
}, {
area: "second",
on: false
}, {
area: "third",
on: false
}, {
area: "fourth",
on: false
}];
$scope.words = [{
name: 'Red',
sound: "audio/test.mp3",
quarter: ['colors']
}, {
name: 'White',
sound: "audio/test.mp3",
quarter: 'colors'
}, {
name: 'Blue',
sound: "audio/test.mp3",
quarter: 'colors'
}, {
name: 'Yellow',
sound: "audio/test.mp3",
quarter: 'colors'
}, {
name: 'Black',
sound: "audio/test.mp3",
quarter: 'colors'
}, {
name: 'Green',
sound: "audio/test.mp3",
quarter: 'colors'
}, {
name: 'One',
sound: "audio/test.mp3",
quarter: 'numbers'
}, {
name: 'Two',
sound: "audio/test.mp3",
quarter: 'numbers'
}, {
name: 'Three',
sound: "audio/test.mp3",
quarter: 'numbers'
}, {
name: 'Four',
sound: "audio/test.mp3",
quarter: 'numbers'
}, {
name: 'Five',
sound: "audio/test.mp3",
quarter: 'numbers'
}, {
name: 'Six',
sound: "audio/test.mp3",
quarter: 'numbers'
}, {
name: 'Seven',
sound: "audio/test.mp3",
quarter: 'numbers'
}, {
name: 'Eight',
sound: "audio/test.mp3",
quarter: 'numbers'
}, {
name: 'Nine',
sound: "audio/test.mp3",
quarter: 'numbers'
}, {
name: 'Ten',
sound: "audio/test.mp3",
quarter: 'numbers'
}, {
name: 'am',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'and',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'are',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'at',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'can',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'do',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'for',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'go',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'have',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'he',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'here',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'I',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'is',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'it',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'like',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'look',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'me',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'my',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'no',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'play',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'said',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'see',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'she',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'the',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'to',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'up',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'we',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'will',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'you',
sound: "audio/test.mp3",
quarter: 'one'
}, {
name: 'secondtest',
sound: "audio/test.mp3",
quarter: 2
}, {
name: 'thirdtest',
sound: "audio/test.mp3",
quarter: 'third'
}, {
name: 'will',
sound: "audio/test.mp3",
quarter: 'fourth'
}];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="sight">
<h4>If you don't want to practice all of the words, please choose the quarter or types you want to practice. You can choose more than one.</h4>
<div ng-controller="WordController">
<li ng-repeat="area in catagoryArray">
<label>
<input type="checkbox" ng-model="area.on">{{area.area}}
</label>{{area.on}}
</li>
<h1>Words</h1>
<ul>
<li ng-repeat="word in words | filter:myFunc">{{word.name}}</li>
</ul>
</div>
</body>
for (var category in $scope.categoryArray) {
var c = $scope.categoryArray[category];
if (c.on && a.quarter instanceof Array &&
a.quarter.indexOf(c.area) > -1) {
return true;
}
if (c.on && a.quarter == c.area) {
return true;
}
}
return false;
普伦克
相关文章:
- 让我自己的过滤器不工作
- 角度过滤器可以工作,但将输入视为未定义并抛出错误
- ng repeat在ng repeat-过滤器不工作
- 异步加载的SVG过滤器feColorMatrix在Chrome中工作,而不是在Safari或Firefox中
- 角度过滤器不工作
- Angular/Ionic/Javascript:无法让下拉过滤器作为复选框工作
- 角度:搜索器(过滤器)根据数据类型工作不同
- Angularjs ng-repeat过滤器在v1.0.1中工作,而不是v1.3.7
- 如果可能的话,我需要帮助了解这个jQuery过滤器函数是如何工作的,逐行工作
- 无法让 Angular 自定义过滤器工作
- Angularjs 过滤器无法按预期工作
- 角度 - 自定义过滤器如何真正工作
- jQuery点击按钮don'在剑道网格中应用过滤器后无法工作
- 角度自定义指令基本输入过滤器工作不正常,只显示匹配的第一个实例
- 选择2过滤器不工作
- 范围过滤器仅在第一个控制器中工作
- 剑道源过滤器不工作
- 为什么我在Meteor反应表中的自定义过滤器不工作
- JQuery表过滤器工作太慢
- 如何使我的复选框过滤器工作