javascript - Controller 的未知提供程序错误

标签 javascript angularjs ionic-framework

这是我的 app.js 文件的样子:

// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'starter.controllers'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    ionic.Platform.fullScreen()
    if (window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      // StatusBar.styleDefault();
      StatusBar.hide();
    }
  });
})

.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider

  .state('app', {
    url: "/app",
    abstract: true,
    templateUrl: "templates/rubyonic/menu.html",
    controller: 'AppCtrl'
  })

  .state('app.alerts', {
    url: "/alerts",
    views: {
      'menuContent': {
        templateUrl: "templates/rubyonic/alerts.html"
      }
    }
  })

  .state('app.profile', {
    url: "/profile",
    views: {
      'menuContent': {
        templateUrl: "templates/rubyonic/profile.html"
      }
    }
  })

  .state('app.rank-charts', {
    url: "/rank_charts",
    views: {
      'menuContent': {
        templateUrl: "templates/rubyonic/rank_charts.html"
      }
    }
  })

  .state('overview', {
    url: "/overview",
    controller: 'OverviewCtrl',
    templateUrl: "templates/rubyonic/overview.html"
  })

  .state('app.claim-details', {
    url: "/claim-details",
    views: {
      'menuContent': {
        templateUrl: "templates/rubyonic/claim_details.html"
      }
    }
  })

  .state('app.scorecards', {
    url: "/scorecards",
    views: {
      'menuContent': {
        templateUrl: "templates/rubyonic/scorecards.html"
      }
    }
  })

  .state('app.fnol', {
    url: "/fnol",
    views: {
      'menuContent': {
        templateUrl: "templates/rubyonic/fnol.html"
      }
    }
  })

  // if none of the above states are matched, use this as the fallback
  $urlRouterProvider.otherwise('/overview');
});

这是我的 controller.js:

angular.module('starter.controllers', [])

.controller('OverviewCtrl', function($scope,Surveys) {
        $scope.surveys = Surveys.all();
})

.controller('NavigationCtrl', function($scope, $state,$ionicHistory) {

  // Function to go states. We're using ng-click="go('app.state')" in all our anchor tags
  $scope.go = function(path){
      // console.log('working. Click was Triggered');
      $state.go(path);
      // console.log($ionicHistory.viewHistory());
  }

  //Function to go back a step using $ionicHistory
  $scope.goBackAStep = function(){
      console.log('clicked');
      $ionicHistory.goBack();
  }

});

这是我的 services.js:

var app = angular.module('starter.services', [])

.factory('Surveys',function(){
    var surveys = [{
      id: 0,
      name: 'Honda Survey Results'
    },

    {
      id: 1,
      name: 'Toyota Survey Results'
    },
    {
      id: 2,
      name: 'BMW Survey Results'
    },
    {
      id: 3,
      name: 'Nissan Survey Results'
    },
    {
      id: 4,
      name: 'Tesla Survey Results'
    },
    {
      id: 5,
      name: 'Mazda Survey Results'
    },
    {
      id: 6,
      name: 'Ford Survey Results'
    },
    {
      id: 7,
      name: 'Apple Survey Results'
    },
    {
      id: 8,
      name: 'Microsoft Survey Results'
    },
    {
      id: 9,
      name: 'IBM Survey Results'
    },
    {
      id: 10,
      name: 'Amazon Survey Results'
    }
    ];
    return {
    all: function() {
      return surveys;
    },

    get: function(surveyId) {
      // Simple index lookup
      return surveys[surveyId];
    }
};
})

我在控制台上收到错误:[$injector:unpr] Unknown provider: SurveysProvider <- Surveys <- OverviewCtrl 错误。我想我在 Controller 中正确注入(inject)了服务,但看不出问题所在。任何帮助将不胜感激

最佳答案

您忘记将 starter.services 指定为模块 starter.controllers 中的依赖项。这很重要,因为您要在 OverviewCtrl Controller 中注入(inject) Surveys

你的 controller.js 应该是这样的:

angular.module('starter.controllers', ['starter.services'])

.controller('OverviewCtrl', function($scope,Surveys) {
        $scope.surveys = Surveys.all();
})

.controller('NavigationCtrl', function($scope, $state,$ionicHistory) {

  // Function to go states. We're using ng-click="go('app.state')" in all our anchor tags
  $scope.go = function(path){
      // console.log('working. Click was Triggered');
      $state.go(path);
      // console.log($ionicHistory.viewHistory());
  }

  //Function to go back a step using $ionicHistory
  $scope.goBackAStep = function(){
      console.log('clicked');
      $ionicHistory.goBack();
  }

});

关于javascript - Controller 的未知提供程序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31238437/

相关文章:

javascript - 如何在MVC中的Jquery中显示二维数组?

javascript - 使用 toISOString() 获取 ng-repeat 中的纪元日期

cordova - Ionic4 watchPosition 和 getCurrentPosition 的地理位置对于 Ionic/Capacitor 不准确

javascript - 根据 Iframe 的 src URL 的 Iframe 内容动态调整 Iframe 的大小

javascript - 引导按钮到新页面

javascript - 如何使用 Angularjs、multipart/form-data 上传文件

javascript - 如何将 $q 的构造函数语法与 Angular $http 的 config.timeout 一起使用?

angularjs - Angular Schematics - 在树上应用数以千计的更改导致错误超出最大调用堆栈大小

javascript - 改变 ionic 清新器的位置

ios - Ionic 4 - Facebook 插件在尝试登录时在 iOS 上返回 'User cancelled.'