Published: 2015-09-27

DropDown Select In Angular

Table of Contents

1 Summary

I am using angularjs as mobile web framework in my project, and when I use dropdown select control I find interesting things.

Enviroment : angularjs 1.4.6, odoo 8.0(server side), pycharm, chrome, ubuntu 14.04

2 Single dropdown select

Firstly, if I want to make a single dropdown select, I using as follows:

<select name="select1" ng-model="model1" ng-options=" as for option1 in options1">
      <option value="">---Pick One---</option>

In this case,options1 is a array containing objects, such as [{id:1,name:'apple'},{id:2,name:'pear'}]. When I choose one, the model1 is binding to Also, the options1 can be a object, or as can write in other ways depend on needs, you can learn more refer to Angularjs doc.

Back to example, When I want to generate the options, data from server is like this: [{id:1,name:'apple'},{id:2,name:'pear'}]., of course remember dump to json. And when I in the browser, It adds $$hashkey to each object, the data becomes this: [{id:1,name:'apple',$$hashKey: "object:123"},{id:2,name:'pear',$$hashKey: "object:124"}],But $$hashkey doesn't affect others. If I want the options selected when the page loaded, I also afferent the id from server side, so I can set the model1 to id to make it selected.

When option is selected, just post the data of model1 to server,so the server knows which one is selected.

3 Multle-select dropdown select

Secondly, I want to multle-select dropdown select. that's simple, just add multiple"multiple"= to HTML conrol like following:

<select name="select2" ng-model="model2" ng-options=" as for option2 in options2" multiple="multiple">
    <option value="">---Pick Many---</option>

From the browser side,simply,the Get data(from server) is like this format: [{id:1,name:'apple'},{id:2,name:'pear'},{id:3,name:'orange'}],(also,angular will add $$hashkey in each object) and if you want pre-selected, also set model2 to id list ,the format like this [1,3]. The Post data is like {ids:"[1,3]"}, rember to tranfer to json using: JSON.stringify() before post to server.

4 Cascading dropdown seletion

Finnally, I want make a cascading dropdown seletion, for example, I want to pick country, state, area in turn, here I make up two ways.

4.1 First solution:

<!-- HTML -->
<select ng-model="selectedCountry" ng-change="selectedUnit=''" ng-options="country as for country in countries" >
    <option value="">---Pick Country---</option>
<select ng-model="selectedState" ng-change="selectedArea=''" ng-options="state as for state in selectedCountry.state">
    <option value="">---Pick State---</option>
<select ng-model="selectedArea" ng-options="area as for area in selectedState.area">
    <option value="">---Pick Area---</option>
#Get data from server like this

#Also,when angularjs get this data(of json format) from server,it tanfer it, and add $$haskkey to each object,including object in object.

So,if i want to pre-select options, I need also get the country id,state id,and area id from the server, and in angularjs controller, use the given id to get object from transfered data(in order to get \[haskkey also, I didn't do this on server because in server data don't contain `\]hashkey`),so the three object may look like this:




In this case,it can work. If you set selectedCountry or selectedState or selectedArea without $$hashkey, It don't show pre-selected data I want.

The problem of this method is:when the data gets more, each time I load this page, the server will caculate to generate this format data(or you can store it depend on your need), and the client side will caculate also to get the hash key.It is slow and waste performance because in most case, the data is the same(also depend on your situation).

What is $$hashkey? I find this answer from stackoverflow: >Angular adds this to keep track of your changes, so it knows when it needs to update the DOM.If you use angular.toJson(obj) instead of JSON.stringify(obj) then Angular will strip out these internal-use values for you.

Also, if you change your repeat expression to use the track by {uniqueProperty} suffix, Angular won't have to add $$hashKey at all. For example

    <li ng-repeat="link in navLinks track by link.href">
        <a ng-href="link.href">{{link.title}}</a>

Just always remember you need the "link." part of the expression - I always tend to forget that. Just track by href will surely not work.

Another helpful: >In my use case (feeding the resulting object to X2JS) the recommended approach

data = angular.toJson(source); help to remove the $$hashKey properties, but the result could then no longer be processed by X2JS.

data = angular.copy(source); removed the $$hashKey properties as well, but the result remained usable as a parameter for X2JS.

4.1.1 Problem left

Then I wonder if I remove $$hashkey all, would it work and be simpler for data processing?

4.2 Second Solution

However, I choose the second solution:

<select ng-model="vm.newCountryId" ng-change="vm.getState()" ng-options=" as for country in vm.countrys" >
    <option value="">--select country--</option>
<select ng-model="vm.newStateId" ng-change="vm.getArea()" ng-options=" as for state in vm.states" >
    <option value="">--select state--</option>
<select ng-model="vm.newAreaId" ng-options=" as for area in vm.areas" >
    <option value="">--select area--</option>

And function (also getCountry()),getState(),vm.getArea() I write in angular controller to get data list from server. So each time when i select a value, it trigger a funtion to get data from server.Also in the funtion I use the Id to point what data I want get from server.The code is some like this:

//  In controller
vm.getArea = function(){

            //Needs attention here

            .then(function (data)
                vm.areas = data;
                $timeout(function () {
                }, 1000);
            function (error) {
                toaster.pop('error', "errormsg", "errormsg...");
//process pre-select data from server. Just get 3 id from the server, and the get one by one from server
    vm.newCountryId = data['country_id'];
        vm.newStateId = data['state_id'];
            vm.newAreaId = data['area_id'];

In this method, Post data is just 3 ids. This is simple, and speed fast comparing the former solution.And the data and logic is also simple and clear.

This is my first blog, maybe it looks verbose and not clearly, I am sorry for that, I will keep trying to make it better.:)

Author: Nisen