如何将ngCookies注入angularAMD define()中的控制器?

时间:2015-12-02 03:55:05

标签: angular-amd

我是AMD新手。这在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: '/'});             
 }]);

对于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);
  });

和我的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: '/'});              
});

为什么我收到此错误:

错误:$ injector:unpr未知提供者:$$ cookieReaderProvider< - $$ cookieReader< - $ cookies

我该如何解决?

1 个答案:

答案 0 :(得分:0)

修正:看来它是angular.js和angular-cookies.js

之间的版本差异