在使用ui-select的时候,用拼音检索报错。
点进去看文件,错误是出现在过滤器中
去看这个过滤器中代码,原因出现在画红框的地方,item[prop]
转化为字符串的时候出错
查看item[prop]
过滤器代码
return function(items, props) {
var out = [];
if (angular.isArray(items)) {
items.forEach(function(item) {
var itemMatches = false;
var keys = Object.keys(props);
for (var i = 0; i < keys.length; i++) {
var prop = keys[i];
var text = props[prop].toLowerCase();
if(item[prop].toString().toLowerCase().indexOf(text) !== -1) {
itemMatches = true;
break;
}
}
if (itemMatches) {
out.push(item);
}
});
} else {
// Let the output be the input untouched
out = items;
}
return out;
};
过滤器的使用代码
<ui-select-choices repeat="department in departments | propsFilter: {name: $select.search, pingyin: $select.search}">
<div ng-bind-html="department.name | highlight: $select.search"></div>
</ui-select-choices>
个人理解:props过滤器的使用是传入一个数组,将输入的拼音转化与data中的拼音匹配从而出结果,当时我的data中拼写的为“pinyin”,而使用中用的“pingyin”
出错点:在data中期待的值为pingyin,而我给出的值为pinyin
如果将data中改为“pinyin”使用中也改为“pinyin”也能解决问题,如下图