2
當我試圖在md-contact-chips上搜索時我有一個錯誤顯示createFilterFor
不是函數。但我確實將它創建爲一個函數,如下所示。我可否知道有什麼方法可以解決這個問題?javascript array.filter()的功能不起作用
基本上我試圖克隆這個demo但不是寫聯繫人從我的數據庫通過使用HTTP調用來替換聯繫人名稱,即顯示以下Account.getTastes()
但是手動的名字我叫現有的數據,之後我得到的數據顯示在像以下圖片那樣的md-contact-chip中,querySearch
不再工作。
HTML
<md-contact-chips required ng-model="tags" md-require-match md-contacts="querySearch($query)" md-contact-name="name" md-contact-image="image" filter-selected="true" placeholder="Thai, chinese, cheap, cafe and etc">
</md-contact-chips>
Controller.js
$scope.querySearch = querySearch;
$scope.filterSelected = true;
$scope.allContacts = loadContacts();
$scope.allContacts.then(function(contacts){
$scope.tags = [contacts[0],contacts[1]]
})
/**
* Search for contacts.
*/
function querySearch (query) {
var results = query ?
$scope.allContacts.filter(createFilterFor(query)) : [];
return results;
}
/**
* Create filter function for a query string
*/
function createFilterFor(query) {
var lowercaseQuery = angular.lowercase(query);
return function filterFn(contact) {
return (contact._lowername.indexOf(lowercaseQuery) != -1);;
};
}
function loadContacts() {
var contacts;
return Account.getTastes()
.then(function(res) {
contacts = res.data[0].tastes;
return contacts.map(function (c, index) {
var colors = ["1abc9c", "16a085", "f1c40f", "f39c12", "2ecc71", "27ae60", "e67e22","d35400","3498db","2980b9","e74c3c","c0392b","9b59b6","8e44ad","34495e","2c3e50"];
var color = colors[Math.floor(Math.random() * colors.length)];
var cParts = c.split(' ');
var contact = {
name: c,
image: "http://dummyimage.com/50x50/"+color+"/f7f7f7.png&text="+c.charAt(0)
};
contact._lowername = contact.name.toLowerCase();
return contact;
});
})
.catch(function(error) {
console.log("Error:"+error)
});
}
你可以試試'的console.log(createFilterFor);前'VAR的結果= ...''對不對? –
@BaliBalo返回功能createFilterFor(查詢) –
嗯,我嘗試了[簡體版](https://jsfiddle.net/t8bm4rxk/)和它的作品所以它可能是你覆蓋'createFilterFor'的東西,是不是函數在你的代碼中的某處。只是看到這部分,無法幫助你。 –