我尝试使用以下代码设置cookie:
angular.module('myApp').controller('myController',['$scope','$http','$cookies',function ($scope,$http,$cookies) { $scope.setMyCookie = function () { $cookies.put('Mykey','MyValue'); }; $scope.setMyCookie(); }]);
我更新到角度cookie的版本1.3.14,我知道有一个重大变化,但我现在应该怎么写上面的代码?
运行上面的代码我得到这个错误:错误:$cookies.put不是一个函数
更新:
我必须在2个文件中执行此操作:
var app = angular.module('myApp',['ngRoute']); app.config(['$routeProvider','$httpProvider',function ($routeProvider,$httpProvider) { }]); angular.module('myApp',['ngCookies']).controller('cookiesExample',['$cookies',function ($cookies) { // Retrieving a cookie var favoriteCookie = $cookies.myFavorite; // Setting a cookie $cookies.myFavorite = 'oatmeal'; }]);
它通过设置$cookies变量发生:
原文链接:https://www.f2er.com/angularjs/143764.htmlangular.module('cookiesExample',['ngCookies']) .controller('ExampleController',function($cookies) { // Retrieving a cookie var favoriteCookie = $cookies.myFavorite; // Setting a cookie $cookies.myFavorite = 'oatmeal'; }]);
你的版本:
angular.module('myApp',['ngCookies']) .controller('myController',$cookies) { // Retrieving a cookie var favoriteCookie = $cookies.myFavorite; // Setting a cookie $cookies.myFavorite = 'oatmeal'; }]);
注意:请记住包含< script src =“angular-cookies.js”>在你的HTML中.