ngcookies angularjs如何使用子控制器包含模块i

时间:2017-07-13 07:12:50

标签: angularjs angularjs-directive

如何将ngCookies服务注入控制器。我得到一个错误说明注射失败如下错误:[$ injector:unpr] http://errors.angularjs.org/1.5.3/ $ injector / unpr?p0 =%24cookiesProvider%20%3C-%20%24cookies%20%3C-%20Intake

app.controller("Intake", ["$scope", "$http", "$window", "$mdDialog", "$mdToast", "IntakeFactory", "fileUpload", 'ngCookies',  function ($scope, $http, $window,  $mdDialog,  $mdToast, $Intake, $fileUpload ,$cookieStore ) {

3 个答案:

答案 0 :(得分:0)

您必须注射ngCookies不在您的控制器中但在您的模块中,并且您的注射顺序不正确。

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

在您的控制器中,您可以通过注入$cookieStore来访问其属性,如下所示

app.controller("Intake", ["$scope", "$http", "$window", "$mdDialog", "$mdToast", "IntakeFactory", "$fileUpload", '$cookieStore',  function ($scope, $http, $window,  $mdDialog,  $mdToast,IntakeFactory, $fileUpload ,$cookieStore )

答案 1 :(得分:0)

你可以像这样使用cookie: (它不会在这个片段中工作,因为这里不允许使用cookies)



var app = angular.module('myApp', ['ngCookies']);
app.controller('myCtrl', function($scope, $cookies) {
  $cookies.putObject('cookieData', 'this is value stored by cookie');
  $scope.data = $cookies.getObject('cookieData');
});

<!DOCTYPE html>
<html>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular-cookies.min.js"></script>

<body>

  <div ng-app="myApp" ng-controller="myCtrl">
    {{data}}
  </div>

</body>

</html>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

正确注入服务。 尝试下面的一个: -

angular.module('Intake', ['ngCookies']).controller('Intake', ['$cookies', function( $cookies){}])