javascript - Using a weaker equality test for angular select -
lot's of advice tells me this:
// in js $scope.items = [ { id: 1, name: 'foo'}, { id: 2, name: 'bar'}]; // in html <select ng-model="selecteditem" ng-options="item item.name item in items"></select>
and works fine. works fine too:
$scope.selecteditem = $scope.items[1];
the select initialized bar object.
but doesn't work:
$scope.selecteditem = { id: 2, name: 'bar'};
the select control not initialized bar object (understandably, think). selecteditem equivalent bar object, not equal it. have problem in app parse back-end. selecteditem pointer 1 object another, , items of (handful) of objects in target class. these in 2 different queries.
is there way manipulate angular still select object, use custom equality test, object id?
yes, require use of external library or scripting of own. need lookup function take key/value pair (such present in code sample) , return item array.
the example below uses findwhere
in underscore, which:
looks through list , returns first value matches of key-value pairs listed in properties.
$scope.selecteditem = _.findwhere($scope.items, {id: 2, name: 'bar'});
using .findwhere, can search subset of key/value pairs contained in array item, so:
$scope.selecteditem = _.findwhere($scope.items, {id: 2});
Comments
Post a Comment