I am trying to make angular search engine interface. The user selects some parameters in the form, clicks "search", and then fills in the URL using the parameters $location.search()
search interface parameters that are used to build the form:
params = { milestones: [ "a", "b", "c", "d", etc. ], properties: [ { "name": "name A", type: "text" }, { "name": "name B", type: "checkbox" }, { etc. } ] }
inside the controller:
$scope.query = $location.search(); // get the parameters from the url $scope.search = function (query) { // set the parameters to the url $location.search(query); };
and html forms
<select ng-model="query.milestone_name" ng-options="ms for ms in params.milestones"> <option value="">-select milestone-</option> </select> <select ng-model="property" ng-options="prop.name for prop in params.properties" ng-change="query.property_name=property.name"> <!-- if the object 'property' was passed in the url, it would look like this `%5Bobject%20Object%5D`, so its 'name' parameter is converted to a string --> <option value="">-select property-</option> </select> <span ng-switch="property.type"> <label ng-switch-when="text">{{query.property_name}}: <input type="text" ng-model="query.property_value"></label> <label ng-switch-when="checkbox">{{query.property_name}}: <input type="checkbox" ng-model="query.property_value"></label> </span> <button ng-click="search(query)">search</button>
and elsewhere on the page is a list of results.
the user can also access the search results page with the following URL:
http://myapp.com/search?milestone_name=a&property_name=name%20A
almost everything works fine: a list of results is displayed, the milestone parameter is pre-selected with the correct value in the select component, but not the property parameter, because it is not a string, it is an object.
How can I set the default (ng-model) of the select component for an object?
or any other idea on how I should do this?
angularjs
François Romain
source share