I am currently utilizing the typeahead angular bootstrap directive, which I find to be quite useful. However, I am facing a challenge regarding obtaining the select value when using it with an array of objects (which is necessary for a custom template). The issue arises where I cannot retrieve the typeahead selection value – while it displays correctly, it is passed as [object object] to the target controller.
Here is the form in question:
<form ng-submit="startSearch(search.term , false, true)" novalidate>
<input typeahead-editable="true" typeahead="item as label(item) for item in startSearch($viewValue, true)| limitTo:10"
required type="text" class="searchInput" ng-model="search.term"
typeahead-template-url="views/search/typeahead.html" /> <!--| filter:$viewValue typeahead-on-select="goState(selectState(select), label(select)"-->
<button class="searchBT" ng-submit="startSearch(search.term , false, true)"><i class="icon-search"></i></button>
Here is the relevant part of the controller:
$scope.search = {};
$scope.startSearch = function(term, suggest, submitted){
var deferred = $q.defer();
if (submitted) {
console.log($scope.search)
$state.go('search.home',{'term': $scope.search.term }); //term gets [object object] instead of the displayed name
} else {
searchService.doSearch(term, suggest).then(function(data){
"use strict";
// console.log('data',data)
if (suggest){
deferred.resolve(data)
}
}, function(err){
"use strict";
$log.debug('err',err);
})
}
return deferred.promise;
}
};
$scope.label = function(item){
"use strict";
//select label
if (!item) {
return;
}
return item.symbol || (item.first_name +" " + item.last_name)
}
In summary of my issue:
Although I receive a correct displayed value from the typeahead select list, it appears that the model (search.term) does not update properly and I end up with a [object object] result. Interestingly, if I manually input a value in the field and submit, I obtain the correct value along with the expected state transition.
To complicate matters further, the list contains various types of objects, so I need to apply certain logic regarding the objects through a function/filter rather than simply extracting a field.
Thank you for your assistance!