简体   繁体   English

Angular:ngCookies的奇怪行为

[英]Angular: strange behavior with ngCookies

In my Angular app 在我的Angular应用中

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

I've defined authCtrl controller: 我已经定义了authCtrl控制器:

mainApp.controller('authCtrl', ['$scope, $cookies',function ($scope, $http, $cookies) {

    $scope.credentials = {};

    $scope.signCheck = function () {
        a = $cookies.getObject('session_credentials');
        console.log(a);
    };
}]);

If I'm removing $scope declaration from array (injection array?) 如果我要从数组中删除$scope声明(注入数组?)

mainApp.controller('authCtrl', ['$cookies',function ($scope, $http, $cookies) {

$scope becomes undefined. $scope变为未定义。 If I'm removing $cookies$cookies becomes undefined. 如果我要删除$cookies ,则$cookies将变得不确定。 If I keep them both — $injector unknown provider error . 如果同时保留它们— $ injector未知提供程序错误

What I'm doing wrong? 我做错了什么?

Just be sure that you indicate the services in a correct order in the injector array and the controller function params: 只要确保您在注射器阵列和控制器功能参数中以正确的顺序指示了服务:

Angular docs says : 角度文档说

This is the preferred way to annotate application components. 这是注释应用程序组件的首选方法。 This is how the examples in the documentation are written. 这就是文档中示例的编写方式。

For example: 例如:

someModule.controller('MyController', ['$scope', 'greeter', function($scope, greeter) {
  // ...
}]);

Here we pass an array whose elements consist of a list of strings (the names of the dependencies) followed by the function itself. 在这里,我们传递一个数组,该数组的元素由字符串列表(依赖项的名称)和函数本身组成。

When using this type of annotation, take care to keep the annotation array in sync with the parameters in the function declaration. 使用这种类型的注释时,请注意使注释数组与函数声明中的参数保持同步。

Perhaps this controller definition will work for you: 也许此控制器定义将为您工作:

mainApp.controller('authCtrl', ['$scope', '$http', '$cookies', function ($scope, $http, $cookies) {

    $scope.credentials = {};

    $scope.signCheck = function () {
        a = $cookies.getObject('session_credentials');
        console.log(a);
    };
}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM