in this example I\'m returning a string depending on whether a text field is a value for \"beast\" and/or \"color\". but this is only returning a string. I would return in two c
Look at this
app.service('myFactoryService', function(){
this.createObj = function(name,type){
var obj = {};
obj.name = name;
obj.type = type;
return obj;
}
});
results.push(myFactoryService.createObj(item.beast,"animal"));
I don't know somehow why two-way binding doesn't permit to show two variables differently, only object, but there is defined service to create an object, then it is created in filter according to both options.
I am not sure what you are trying to get. Have a look at this plunk. Is this what you wanted ?
It works if you push the whole item to the results list like:
results.push(item);
http://plnkr.co/edit/xW9hqC1z1rKddRWaP2g6?p=preview
You can return objects inside the filter, exactly as you have written them on your question (also, I have cleaned up a bit the ifs... don't use document.getElementById inside the filter, you have the searchText in there already as second parameter of the filter!):
app.filter('searchData', function() {
return function(items, searchText) {
var results = [];
if(searchText) {
angular.forEach(items, function(item) {
if(item.beast.indexOf(searchText) === 0) {
results.push({ name: item.beast, type: 'animal' });
}
if(item.color.indexOf(searchText) === 0) {
results.push({ name: item.color, type: 'color of animal' });
}
});
}
return results;
};
});
And then, you can use this on the P object of the html:
<p ng-repeat = "item in data | searchData : search">{{item.name + ' and ' + item.type}}</p>
I have forked the plunkr here with the changed code:
http://plnkr.co/edit/EDd578?p=preview
Hope it helps,
Best regards,
Rafa.
ps. Also, remember always to use the equality operators !== or === in Javascript, and not the != and == forms.