离子未知提供者Service.js Ionic

时间:2016-08-14 11:15:58

标签: angularjs ionic-framework

我只是在学习离子。我想为我的控制器提供服务。 但是当我制作和运行时,会出现这样的错误:

ionic.bundle.js:26794 Error: [$injector:unpr] Unknown provider: scoreServiceProvider <- scoreService <- datarateCtrl

这是我的service.js代码:

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

.factory('FirstService', function($http) {
var baseUrl = 'http://192.168.0.101/xxx/grabdata/';
return {
    getAll: function() {
        return $http.get(baseUrl+'select.php');
    },
    getId: function (beritaId){
        return $http.get(baseUrl+'select_id.php?id='+beritaId); 
    }

};

});

var StudentService = angular.module('ionicApp', [ionic])
StudentService.factory('scoreService', function($http) {
var baseUrl = 'http://192.168.0.101/xxx/grabdata/';
return {
    getAll: function() {
        return $http.get(baseUrl+'selectxxxx.php');
    }

};

});

这是我的controller.js代码:

.controller('datarateCtrl', function($scope,$state, scoreService,$timeout, $ionicLoading){
$scope.showData = function() {
  scoreService.getAll().success(function(data) {
        $scope.datarate = data;
    }).finally(function() {
        $scope.$broadcast('scroll.refreshComplete');
    });
};


$scope.reload = function (){
    $state.go('tab.klasemen');
};

// Setup the loader
  $scope.loading = $ionicLoading.show({
content: '<i class="icon ion-load-a"></i>',
animation: 'fade-in',
showBackdrop: true,
maxWidth: 50,
showDelay: 0
  });

  $timeout(function () {
$scope.showData();
$ionicLoading.hide();
 }, 2000);

})

这是我的html代码(klasemen.html)

<html ng-app="ionicApp">
 <ion-header-bar class="bar bar-header bar-positive" align-title="center">
    <h1 class="title">xxxx</h1>
 </ion-header-bar>

<ion-view>
<ion-content padding="false" class="has-header">
    <ion-refresher
        pulling-text="Pull to refresh..."
        on-refresh="showData()">
      </ion-refresher>

            <div class="row header">
                  <div class="col">xx</div>
                  <div class="col">xx</div>
                  <div class="col">xx</div>
                  <div class="col">xx</div>
                  <div class="col">xx</div>
                  <div class="col">xx</div>
            </div>
            <div class="row" ng-repeat="data in datarate">
                  <div class="col">{{data.xx}}</div>
                  <div class="col">{{data.xx}}</div>
                  <div class="col">{{data.xx}}</div>
                  <div class="col">{{data.xx}}</div>
                  <div class="col">{{data.xx}}</div>
                  <div class="col">{{data.xx}}</div>
            </div>
</ion-content>

这是我的app.js

.state('tab.klasemen', {
  url: '/klasemen',
  views: {
    'tab-klasemen': {
      templateUrl: 'templates/klasemen.html',
      controller: 'datarateCtrl'
    }
  }
})

任何人都可以告诉我错过了什么或我的代码有什么错误吗? 非常感谢你们。

1 个答案:

答案 0 :(得分:0)

在您的controller.js中,您的代码应该是这样的。

angular.module('ionicApp')
.controller('datarateCtrl', function($scope,$state, scoreService,$timeout, $ionicLoading){

   // script goes here
   //$scope.showData

 });