控制器

时间:2015-07-06 05:41:43

标签: javascript angularjs ionic

这是我的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]未知提供程序:SurveysProvider&lt; - Surveys&lt; - 我的控制台上的OverviewCtrl错误。我想我正在控制器中正确注入服务而不知道问题所在。任何帮助都会非常感激

2 个答案:

答案 0 :(得分:5)

您忘记在模块starter.services中指定starter.controllers作为依赖项。这很重要,因为您在Surveys控制器中注入了OverviewCtrl

这就是你的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();
  }

});

答案 1 :(得分:1)

将服务模块注入另一个模块作为您要访问它的依赖项。

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

您正在使用控制器模块中的服务,因此您还需要声明依赖项。