Priyanka Lopes Priyanka Lopes - 1 month ago 14
HTML Question

saving dropdown selection in cookie

I am totally new to Bootstrap and angularjs world and I am stuck at this.
I have a simple select like this below

<div class="pull-left margin-left-20">
<select id="ddlPageSize" class="form-control form-ddl-adj"
ng-model="params.settings().countSelected"
ng-options="item.value as item.text for item in params.settings().countOptions"
ng-change="params.count(params.settings().countSelected)"></select>
</div>


In the controller, I have select values like this

var pageSizeList = [
{ value: 5, text: "5" },
{ value: 10, text: "10" },
{ value: 25, text: "25" },
{ value: 50, text: "50" },
{ value: 100, text: "100" }
];


5 is the option by default. Can some one tell me how to save user selection (say 10) in a cookie so that next time 10 should be the pagesize

Answer

You can use ngCookies module of AngularJS. You'll have to include angular-cookies.js in your code: https://code.angularjs.org/1.4.3/angular-cookies.min.js

Include ngCookies in your module declaration:

angular.module('testApp', ['ngCookies'])

Then you can save a cookie as:

$cookies.put('someKey', 'someVal');

You can retrieve a cookie as:

$cookies.get('someKey')

Updated: Check this fiddle: http://jsfiddle.net/3xyf1bzo/