使用yo angular创建服务:factory test无法加载testProvider

时间:2014-02-25 03:59:19

标签: javascript node.js angularjs angularjs-factory

我正在尝试使用角度fullstack生成器将工厂注入控制器,并且遇到了找不到提供商的问题,我哪里出错了? 我用

创建了一个应用程序

npm install -g generator-angular-fullstack

yo angular-fullstack chatApp

哟角度:工厂测试

应用程序/脚本/服务/ test.js

'use strict';

angular.module('chatApp')
.factory('test', function () {
// Service logic
// ...

 var meaningOfLife = 42;

 // Public API here
  return {
  someMethod: function () {
    return meaningOfLife;
  }
  };
  });

所以我在哪里加载工厂我试图加载main.js文件,如此

应用程序/脚本/控制器/ main.js

'use strict';

angular.module('chatApp')
 .controller('MainCtrl', function ($scope, test) {
console.log(test);
   $scope.messages = 'this is a hardcoded test';
 });

应用程序/脚本/ main.js

'use strict';

angular.module('chatApp', [
'ngCookies',
'ngResource',
'ngSanitize',
'ngRoute',

])
.config(function ($routeProvider, $locationProvider, $httpProvider) {
 $routeProvider
  .when('/', {
    templateUrl: 'partials/main',
    controller: 'MainCtrl'
  })
  .when('/login', {
    templateUrl: 'partials/login',
    controller: 'LoginCtrl'
  })
  .when('/signup', {
    templateUrl: 'partials/signup',
    controller: 'SignupCtrl'
  })
  .when('/settings', {
    templateUrl: 'partials/settings',
    controller: 'SettingsCtrl',
    authenticate: true
  })
  .when('/about', {
    templateUrl: 'partials/about',
    controller: 'SettingsCtrl'
  })
  .otherwise({
    redirectTo: '/'
  });

 $locationProvider.html5Mode(true);

 // Intercept 401s and redirect you to login
 $httpProvider.interceptors.push(['$q', '$location', function($q, $location) {
  return {
    'responseError': function(response) {
      if(response.status === 401) {
        $location.path('/login');
        return $q.reject(response);
      }
      else {
        return $q.reject(response);
      }
    }
   };
   }]);
 })
.run(function ($rootScope, $location, Auth) {

 // Redirect to login if route requires auth and you're not logged in
 $rootScope.$on('$routeChangeStart', function (event, next) {

  if (next.authenticate && !Auth.isLoggedIn()) {
    $location.path('/login');
  }
  });

  console.log($location);

});

错误是:

304 Not Modified
    14ms    
angular.js (line 7997)

Error: [$injector:unpr] Unknown provider: testProvider <- test
http://errors.angularjs.org/1.2.11/$injector/unpr?p0=testProvider%20%3C-%20test
minErr/<@http://localhost:9000/bower_components/angular/angular.js:78
createInjector/providerCache.$injector<@http://localhost:9000/bower_components/angular/angular.js:3543
getService@http://localhost:9000/bower_components/angular/angular.js:3670
createInjector/instanceCache.$injector<@http://localhost:9000/bower_components/angular/angular.js:3548
getService@http://localhost:9000/bower_components/angular/angular.js:3670
invoke@http://localhost:9000/bower_components/angular/angular.js:3697
instantiate@http://localhost:9000/bower_components/angular/angular.js:3718
@http://localhost:9000/bower_components/angular/angular.js:6777
ngViewFillContentFactory/<.link@http://localhost:9000/bower_components/angular-route/angular-route.js:907
nodeLinkFn@http://localhost:9000/bower_components/angular/angular.js:6228
compositeLinkFn@http://localhost:9000/bower_components/angular/angular.js:5637
publicLinkFn@http://localhost:9000/bower_components/angular/angular.js:5542
boundTranscludeFn@http://localhost:9000/bower_components/angular/angular.js:5656
controllersBoundTransclude@http://localhost:9000/bower_components/angular/angular.js:6248
update@http://localhost:9000/bower_components/angular-route/angular-route.js:865
Scope.prototype.$broadcast@http://localhost:9000/bower_components/angular/angular.js:12245

1 个答案:

答案 0 :(得分:0)

我解决了这个问题 我正在使用错误的发电机h 正确的是 哟angular-fullstack:工厂测试 没有你删除功能所以你必须手动删除创建的.js文件和注入main.html的内容