2016-09-15 64 views
0

如何將cookie添加到我的控制器是否正確?

當我使用$ cookie時發生錯誤,否則一切正常。

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

錯誤

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

+0

你會得到什麼錯誤? – sp00m

+0

@ sp00m我加入了我的文章:) –

回答

1

如果你去的錯誤給你的網址:https://docs.angularjs.org/error/$injector/unpr?p0=$cookiesProvider%20%3C-%20$cookies%20%3C-%20loginCtrl

你的角度應用程序是無法找到你的依賴。你有沒有安裝角度曲奇文件?

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'); 
});