保存Cookie

时间:2015-08-10 04:34:35

标签: javascript html angularjs

我对Bootstrap和angularjs世界全新,我对此感到困惑。 我有一个简单的选择,如下所示

<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>

在控制器中,我有选择这样的值

var pageSizeList = [
                        { value: 5, text: "5" },
                        { value: 10, text: "10" },
                        { value: 25, text: "25" },
                        { value: 50, text: "50" },
                        { value: 100, text: "100" }
                      ];
默认情况下,

5是选项。有人可以告诉我如何在cookie中保存用户选择(例如10),以便下次10应该是pagesize

3 个答案:

答案 0 :(得分:1)

您可以使用AngularJS的ngCookies模块。您必须在代码中包含angular-cookies.js:https://code.angularjs.org/1.4.3/angular-cookies.min.js

在模块声明中包含ngCookies

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

然后您可以将Cookie保存为:

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

您可以将Cookie检索为:

$cookies.get('someKey')

更新: 检查这个小提琴:http://jsfiddle.net/3xyf1bzo/

答案 1 :(得分:1)

您可以使用ng-cookies

执行此操作

下拉选择代码html:

<div class="custom-param" ng-controller="themectrl">
          <label id="default-lang" class="label">Default Theme:</label>
            <select ng-change="themeChange(theme)" ng-model="theme" ng-options="theme.url as theme.name for theme in themes">
            </select>
          </div>

angularJS代码:

var app = angular.module('themectrl', ['pascalprecht.translate','ngCookies'] );

app.controller('themeCtrl', function( $scope , $cookies ) {

      $scope.theme = 'theme1.value';

      // create the list of bootswatches
      $scope.themes = [
        { name: 'Theme 1', url: 'theme1.value' },
        { name: 'Theme 2', url: 'theme2.value' }
      ];

        // on dropdown change function
        $scope.themeChange = function($selectedTheme) {
            $cookies.put('ppcdfavtheme', $selectedTheme);
            $scope.theme = $selectedTheme;
        }
});

答案 2 :(得分:-1)

您可以使用持久性使用本地存储来保存您的数据,如果您需要我留下一个带有工作示例的链接祝你好运AngularJS: Real Time Model Persistence Using Local Storage