将某些值保存为cookie

时间:2021-05-15 21:44:45

I am fetching JSON data using $http, and I am displaying it using ng-repeat (table format) I want to add a button to every table data, so on click that value will be saved as a cookie. Please help me out here

我使用$http获取JSON数据,并使用ng-repeat(表格格式)显示它,我想向每个表数据添加一个按钮,因此单击该值将被保存为cookie。请帮我一下

1 个解决方案

#1


2  

View:

观点:

<script src="~/Scripts/angular.js"></script>
<script type="text/javascript">

    function createCookie(name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        }
        else var expires = "";
        document.cookie = name + "=" + value + expires + "; path=/";
    }

    var myApp = angular.module("myApp", []);

    myApp.controller('controller', function ($scope, $http, $compile) {

        $scope.employees = [{ "name": "Employee1", "surname": "Surname1" },
                            { "name": "Employee2", "surname": "Surname2" },
                            { "name": "Employee3", "surname": "Surname3" },
                            { "name": "Employee4", "surname": "Surname4" }];

        $scope.AddCookie = function (employee) {

            debugger;
            var cookie = employee.name + " " + employee.surname;
            createCookie("Employee", cookie, 1);

        }
    });

</script>

<div ng-app="myApp" ng-controller="controller">
    <table>
        <thead>
            <tr>
                <th>Name</th>
                <th>Surname</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="e in employees">
                <td>{{e.name}}</td>
                <td>{{e.surname}}</td>
                <td><input type="button" value="Add Cooke" ng-click="AddCookie(e)"/></td>
            </tr>
        </tbody>
    </table>
</div>

And here is your cookie:

这是你的饼干:

将某些值保存为cookie

#1


2  

View:

观点:

<script src="~/Scripts/angular.js"></script>
<script type="text/javascript">

    function createCookie(name, value, days) {
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
            var expires = "; expires=" + date.toGMTString();
        }
        else var expires = "";
        document.cookie = name + "=" + value + expires + "; path=/";
    }

    var myApp = angular.module("myApp", []);

    myApp.controller('controller', function ($scope, $http, $compile) {

        $scope.employees = [{ "name": "Employee1", "surname": "Surname1" },
                            { "name": "Employee2", "surname": "Surname2" },
                            { "name": "Employee3", "surname": "Surname3" },
                            { "name": "Employee4", "surname": "Surname4" }];

        $scope.AddCookie = function (employee) {

            debugger;
            var cookie = employee.name + " " + employee.surname;
            createCookie("Employee", cookie, 1);

        }
    });

</script>

<div ng-app="myApp" ng-controller="controller">
    <table>
        <thead>
            <tr>
                <th>Name</th>
                <th>Surname</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="e in employees">
                <td>{{e.name}}</td>
                <td>{{e.surname}}</td>
                <td><input type="button" value="Add Cooke" ng-click="AddCookie(e)"/></td>
            </tr>
        </tbody>
    </table>
</div>

And here is your cookie:

这是你的饼干:

将某些值保存为cookie