簡體   English   中英

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

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

我是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

以及如何解決?

固定:似乎這是angular.js和angular-cookies.js之間的版本差異

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM