cookies添加到控制器 - $ cookies

时间:2016-09-15 13:40:20

标签: angularjs

如何将cookie添加到我的控制器中?

当我使用$ cookie时出现错误,否则一切正常。

angular.module('app', ['ngCookies']);
app.controller('loginCtrl', function ($scope, $http, $state, Authorization, $cookies) {
$cookies.put('expires', 'example');
});

错误

  

angular.js:13920错误:[$ injector:unpr]   http://errors.angularjs.org/1.5.8/ $注射器/ unpr?P0 =%24cookiesProvider%20%3 C-%20%24cookies%20%3 C-%20loginCtrl       在错误(本机)       在http://localhost:64757/Scripts/angular.min.js:6:412       在http://localhost:64757/Scripts/angular.min.js:43:174       在Object.d [as get](http://localhost:64757/Scripts/angular.min.js:40:432)       在http://localhost:64757/Scripts/angular.min.js:43:236       在d(http://localhost:64757/Scripts/angular.min.js:40:432)       在e(http://localhost:64757/Scripts/angular.min.js:41:158)       在Object.instantiate(http://localhost:64757/Scripts/angular.min.js:42:24)       在http://localhost:64757/Scripts/angular.min.js:90:32       在q(http://localhost:64757/Scripts/AngularUI/ui-router.min.js:7:14338

1 个答案:

答案 0 :(得分:1)

如果您转到错误提供给您的网址:https://docs.angularjs.org/error/$injector/unpr?p0=$cookiesProvider%20%3C-%20$cookies%20%3C-%20loginCtrl

您的角度应用无法找到您的依赖项。你安装了角度cookie文件吗?您可以使用以下命令安装它:

npm install angular-cookies

OR

bower install angular-cookies

同样,当您声明角度应用程序时,您应该将其分配给变量,然后将其用于控制​​器。

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

app.controller('loginCtrl', function ($scope, $http, $state, Authorization, $cookies) {
    $cookies.put('expires', 'example');
});