无法使Cookie运行起来

时间:2015-12-07 18:11:47

标签: javascript angularjs cookies scope

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

app.config(['$routeProvider',
 function($routeProvider) {


  $routeProvider.

      when('/', {templateUrl: 'main.html'}).

      when('/About', {templateUrl: 'about.html'}).
      when('/Evo', {templateUrl: 'evo.html'}).
      when('/Evo1', {templateUrl: 'evo1.html'}).
      when('/ContactUs', {templateUrl: 'contactus.html', controller: 'projectCtrl'}).
      when('/NewProject', {templateUrl: 'new.html', controller: 'storeProjectCtrl'}).
      when('/Register', {templateUrl: 'register.html', controller: 'register'}).
      when('/Login', {templateUrl: 'login.html', controller: 'loginCtrl'}).
      when('/UpdateProject/:id', {templateUrl: 'views/edit.html', controller: 'editProjectCtrl'}).
      otherwise({redirectTo: '/'});

我正在使用loginCtrl,检查SQL并返回True或False变量。

app.controller( ' loginCtrl ', [' $scope ', ' $http ', ' $location ', ' $cookie ', 

function($scope, $http,$location,$cookieStore)  {   


    $scope.getFormData = function(data) {
        console.log(data.username);//getting data input by user
        console.log(data.password);//getting data input by user
        $http.post('php/login.php', data).success(function(data) {
            console.log(data);


            if (data) {//row inserted

              $scope.insertMessage = "Data inserted";
              $cookies.cookieStore = (data);

            } else {
              // if unsuccessful, bind success message to message
              $scope.insertMessage = "Data incorrect";


            }
            //reset values in form to empty
            console.log($window.sessionStorage.token);
    })  

Iit给了我这个错误:

  

错误:[$ injector:unpr] http://errors.angularjs.org/1.2.12/ $ injector / unpr?p0 = ookieProvider%20%3C-%20%24cookie

一旦我删除$ cookie,错误就会消失。

我确实链接了angular-cookie脚本,还有任何帮助吗?

3 个答案:

答案 0 :(得分:0)

可注射对象应为$cookies而不是$cookie

app.controller( 'loginCtrl ', ['$scope' , '$http', '$location', '$cookies', //<-- change here
    function($scope, $http, $location, $cookies)  {  

        //other controller code should  be as is.

    }
]);

此外,您使用了错误的语法来设置cookie $cookies.cookieStore = (data);,而不是使用$cookies.get&amp;获取和设置数据时$cookies.put

$cookies.set('myData', data); //use while setting data
$cookies.get('myData'); //use while getting data

答案 1 :(得分:0)

我创建了一个jsfiddle here,它显示了putget Cookie的简单方法。

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

app.controller( 'loginCtrl', ['$scope' , '$http', '$cookies',
    function($scope, $http, $cookies)  {  
      $cookies.put('myData', 'data'); //use while setting data
            var myCookie = $cookies.get('myData'); //use while getting data
            console.log(myCookie);
    }
]);

答案 2 :(得分:0)

问题在于angular-cookies.js文件。这是不兼容的运行5.2版本测试版

切换到以前的版本并像魅力一样工作