简体   繁体   English

如何在AngularAMD define()中将ngCookies注入控制器?

[英]How do I inject ngCookies into a controller within an angularAMD define()?

I am new to AMD. 我是AMD的新手。 This worked before AMD like this: 这在AMD之前是这样工作的:

angular.module('app')
  .controller('RegisterCtrl', ['$scope', '$location', '$http', '$cookies', '$timeout', 'myConfig', 
  function($scope, $location, $http, $cookies, $timeout, myConfig) {
    this.$cookies = $cookies;
    this.$timeout = $timeout;

    // Retrieving a cookie
    $scope.cookielocation = $cookies.get('Location');

    // Delete a cookie
    $cookies.remove('Location', {path: '/'});             
 }]);

With AMD, this is what I have put together: app.js: 对于AMD,这是我的汇总:app.js:

define(['angularAMD', 'angular-route', 'angular-cookies', 'banner', 'config', 'services', 'nullSP', 'timeAgo'], function (angularAMD) {
  var app = angular.module("app", ['ngRoute', 'ngCookies']);

  app.config(function ($routeProvider, $locationProvider) {
  console.log("Enter route config");
  $routeProvider
  .when("/", angularAMD.route({
    controller: 'HomeCtrl',
    templateUrl: 'partials/welcome.html',
    controllerUrl: 'controller_welcome'
  }))
  .when("/register", angularAMD.route({
    controller: 'RegisterCtrl', 
    templateUrl: 'partials/register.html', 
    controllerUrl: 'controller_register'
  }))
  .when("/dashboard", angularAMD.route({
    controller: 'DashboardCtrl',
    templateUrl: 'partials/dashboard.html',
    controllerUrl: 'controller_dashboard'
  }))
  .otherwise({
    redirectTo: '/'
  });
   $locationProvider.html5Mode(true);
  });
    app.run(function ($browser) {
      $browser.baseHref = function() { return window.location.pathname  };
     });

    return angularAMD.bootstrap(app);
  });

and my controller_register.js : 和我的controller_register.js:

define(['app', 'angular-cookies'], function (app, cookies) {
  app.controller('RegisterCtrl', ['$rootScope', '$scope', '$location', '$http', '$cookies', '$timeout', 'config',
   function($rootScope, $scope, $location, $http, $cookies, $timeout, config) {

   // Retrieving a cookie
   $scope.cookielocation = $cookies.get('Location');

   // Delete a cookie
   $cookies.remove('Location', {path: '/'});              
});

Why am I getting this error: 为什么会出现此错误:

Error: $injector:unpr Unknown provider: $$cookieReaderProvider <- $$cookieReader <- $cookies 错误:$ injector:unpr未知提供程序:$$ cookieReaderProvider <-$$ cookieReader <-$ cookies

and how do I fix it? 以及如何解决?

固定:似乎这是angular.js和angular-cookies.js之间的版本差异

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

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