2017-04-06 10 views
1

$ resourceサービスを使用してjson serverからデータをフェッチしています。$ httpで正常に動作しています。 app.js


          
  
app.js 
 

 
    'use strict'; 
 
    angular.module('myApp', [ 
 
     'ui.router', 
 
     'ngResource', 
 
     'myApp.version' 
 
    ]) 
 
    .config(['$urlRouterProvider','$stateProvider', function($urlRouterProvider, $stateProvider) { 
 
     $urlRouterProvider.otherwise('/viewDashboard'); 
 
     $stateProvider.state('viewDashboard', { 
 
      url: '/viewDashboard', 
 
      templateUrl: '../viewDashboard/viewDashboard.html', 
 
      controllerAs: '', 
 
      controller: '' 
 
     }); 
 
     $stateProvider.state('viewAboutus', { 
 
      url: '/viewAboutus', 
 
      templateUrl: '../viewAboutus/viewAboutus.html', 
 
      controllerAs: '', 
 
      controller: '' 
 
     }); 
 
     $stateProvider.state('viewForm', { 
 
      url: '/viewForm', 
 
      templateUrl: '../viewForm/viewForm.html', 
 
      controllerAs: 'form', 
 
      controller: 'formController' 
 
     }); 
 
     $stateProvider.state('viewReport', { 
 
      url: '/viewReport', 
 
      templateUrl: '../viewReport/viewReport.html', 
 
      controllerAs: 'report', 
 
      controller: 'reportController' 
 
     }); 
 
    }]); 
 

 

 

 
    Controller: 
 
    Report.js 
 

 
     angular.module('myApp') 
 
    .controller('reportController', function ($scope, reportCandidate) { 
 
     getRecord(); 
 
     function getRecord() { 
 
      reportCandidate.getRecord() 
 
       .success(function (response) { 
 
        $scope.candidateInfo = response; 
 
        //this.candidateInfo = response; 
 
       }) 
 
       .error(function (error) { 
 
        $scope.status = 'Unable to load candidate data: ' + error.message; 
 
       }); 
 
     } 
 
    }) 
 

 
    .factory('reportCandidate', ['$resource', function ($resource) { 
 
     var urlBase = 'http://localhost:3000/records'; 
 
     var reportCandidate = {}; 
 
     reportCandidate.getRecord = function() { 
 
      return $resource('http://localhost:3000/records', {});; 
 
     }; 
 
     return reportCandidate; 
 
    }]);
index.html: 
 

 

 

 
<!DOCTYPE html> 
 
    <html lang="en" ng-app="myApp" class="no-js"> 
 
    <head> 
 
     <meta charset="utf-8"> 
 
     <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
 
     <title>Merchant Demo App</title> 
 
     <meta name="description" content=""> 
 
     <meta name="viewport" content="width=device-width, initial-scale=1"> 
 
     <link rel="stylesheet" href="bower_components/html5-boilerplate/dist/css/normalize.css"> 
 
     <link rel="stylesheet" href="bower_components/html5-boilerplate/dist/css/main.css"> 
 
     <link rel="stylesheet" type="text/css" href="lib/bootstrap.css"> 
 
     <link rel="stylesheet" href="app.css"> 
 
     <script src="bower_components/html5-boilerplate/dist/js/vendor/modernizr-2.8.3.min.js"></script> 
 
     <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" /> 
 
     <link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.0/css/font-awesome.css" /> 
 
     <script type="text/javascript" src="lib/jquery-3.2.0.js"></script> 
 
     <script src="lib/bootstrap.js"></script> 
 
    </head> 
 
    <body> 
 
    <nav class="navbar navbar-default"> 
 
     <div class="container"> 
 
      <div class="navbar-header"> 
 
      <a class="navbar-brand" href="/"><img src="img/logo.png"/></a> 
 
      </div> 
 
      <ul class="nav navbar-nav navbar-right"> 
 
      <li><a ui-sref="viewDashboard"><i class="fa fa-tachometer"></i>Dashboard</a></li> 
 
      <li><a ui-sref="viewAboutus"><i class="fa fa-shield"></i>AboutUs</a></li> 
 
      <li><a ui-sref="viewForm"><i class="fa fa-comment"></i>Form</a></li> 
 
      <li><a ui-sref="viewReport"><i class="fa fa-shield"></i>Report</a></li> 
 
      </ul> 
 
     </div> 
 
     </nav> 
 
     <div ui-view></div> 
 
     <footer class="footer navbar-fixed-bottom text-center"> 
 
     <h5>© 1994-2017 Mastercard. Mastercard is an Equal Opportunity Employer.</h5> 
 
     </footer> 
 
    </body> 
 
     <script src="bower_components/angular/angular.js"></script> 
 
     <script src="bower_components/angular-route/angular-route.js"></script> 
 
     <script src="bower_components/angular/angular-resource.js"></script> 
 
     <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.4.2/angular-ui-router.js"></script> 
 
     <script src="app.js"></script> 
 
     <script src="viewDashboard/viewDashboard.js"></script> 
 
     <script src="viewAboutus/viewAboutus.js"></script> 
 
     <script src="viewForm/viewForm.js"></script> 
 
     <script src="viewReport/viewReport.js"></script> 
 
     <script src="components/version/version.js"></script> 
 
     <script src="components/version/version-directive.js"></script> 
 
     <script src="components/version/interpolate-filter.js"></script> 
 
    </html>

のindex.html:以下 は私のコードです

<!DOCTYPE html> 
<html lang="en" ng-app="myApp" class="no-js"> 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <title>Merchant Demo App</title> 
    <meta name="description" content=""> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="bower_components/html5-boilerplate/dist/css/normalize.css"> 
    <link rel="stylesheet" href="bower_components/html5-boilerplate/dist/css/main.css"> 
    <link rel="stylesheet" type="text/css" href="lib/bootstrap.css"> 
    <link rel="stylesheet" href="app.css"> 
    <script src="bower_components/html5-boilerplate/dist/js/vendor/modernizr-2.8.3.min.js"></script> 
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" /> 
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.0/css/font-awesome.css" /> 
    <script type="text/javascript" src="lib/jquery-3.2.0.js"></script> 
    <script src="lib/bootstrap.js"></script> 
</head> 
<body> 
<nav class="navbar navbar-default"> 
    <div class="container"> 
     <div class="navbar-header"> 
     <a class="navbar-brand" href="/"><img src="img/logo.png"/></a> 
     </div> 
     <ul class="nav navbar-nav navbar-right"> 
     <li><a ui-sref="viewDashboard"><i class="fa fa-tachometer"></i>Dashboard</a></li> 
     <li><a ui-sref="viewAboutus"><i class="fa fa-shield"></i>AboutUs</a></li> 
     <li><a ui-sref="viewForm"><i class="fa fa-comment"></i>Form</a></li> 
     <li><a ui-sref="viewReport"><i class="fa fa-shield"></i>Report</a></li> 
     </ul> 
    </div> 
    </nav> 
    <div ui-view></div> 
    <footer class="footer navbar-fixed-bottom text-center"> 
    <h5>© 1994-2017 Mastercard. Mastercard is an Equal Opportunity Employer.</h5> 
    </footer> 
</body> 
    <script src="bower_components/angular/angular.js"></script> 
    <script src="bower_components/angular-route/angular-route.js"></script> 
    <script src="bower_components/angular/angular-resource.js"></script> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.4.2/angular-ui-router.js"></script> 
    <script src="app.js"></script> 
    <script src="viewDashboard/viewDashboard.js"></script> 
    <script src="viewAboutus/viewAboutus.js"></script> 
    <script src="viewForm/viewForm.js"></script> 
    <script src="viewReport/viewReport.js"></script> 
    <script src="components/version/version.js"></script> 
    <script src="components/version/version-directive.js"></script> 
    <script src="components/version/interpolate-filter.js"></script> 
</html> 


Controller: 
Report.js 

    angular.module('myApp') 
.controller('reportController', function ($scope, reportCandidate) { 
    getRecord(); 
    function getRecord() { 
     reportCandidate.getRecord() 
      .success(function (response) { 
       $scope.candidateInfo = response; 
       //this.candidateInfo = response; 
      }) 
      .error(function (error) { 
       $scope.status = 'Unable to load candidate data: ' + error.message; 
      }); 
    } 
}) 

.factory('reportCandidate', ['$resource', function ($resource) { 
    var urlBase = 'http://localhost:3000/records'; 
    var reportCandidate = {}; 
    reportCandidate.getRecord = function() { 
     return $resource('http://localhost:3000/records', {});; 
    }; 
    return reportCandidate; 
}]); 

しかし、私は以下のエラーが取得しています:

Uncaught TypeError: angular.module(...).info is not a function 
    at angular-resource.js:445 
    at angular-resource.js:858 
(anonymous) @ angular-resource.js:445 
(anonymous) @ angular-resource.js:858 
angular.js:14199 Error: [$injector:unpr] Unknown provider: $resourceProvider <- $resource <- reportCandidate 
http://errors.angularjs.org/1.5.11/$injector/unpr?p0=%24resourceProvider%20%3C-%20%24resource%20%3C-%20reportCandidate 
    at http://localhost:8000/bower_components/angular/angular.js:68:12 
    at http://localhost:8000/bower_components/angular/angular.js:4563:19 
    at Object.getService [as get] (http://localhost:8000/bower_components/angular/angular.js:4716:32) 
    at http://localhost:8000/bower_components/angular/angular.js:4568:45 
    at getService (http://localhost:8000/bower_components/angular/angular.js:4716:32) 
    at injectionArgs (http://localhost:8000/bower_components/angular/angular.js:4741:58) 
    at Object.invoke (http://localhost:8000/bower_components/angular/angular.js:4763:18) 
    at Object.enforcedReturnValue [as $get] (http://localhost:8000/bower_components/angular/angular.js:4609:37) 
    at Object.invoke (http://localhost:8000/bower_components/angular/angular.js:4771:19) 
    at http://localhost:8000/bower_components/angular/angular.js:4569:37 <div ui-view="" class="ng-scope"> 

DNTを問題が何であるかを知っています。誰も私に同じことを助けてもらえますか?

答えて

1

$resourceサービスは、あなたが

.factory('reportCandidate', ['$resource', function ($resource) { 

     return $resource('http://localhost:3000/records', {});; 

    }]); 

にあなたの工場に変更し、コントローラにあなたが

を行うことができれば実際に query方法

reportCandidate.getRecord().query(function(results){ 
console.log(results); 
}) 

を使用する必要があるサーバーからデータを照会するための独自のメソッドを持っています

reportCandidate.query(function(results){ 
    console.log(results); 
}) 
+0

ありがとうございました!それは私のために働いた:) – Raj

2
Uncaught TypeError: angular.module(...).info is not a function 
    at angular-resource.js:445 
    at angular-resource.js:858 

このエラーは通常、モジュールファイルが一致しない場合に発生し、angular-resource.jsangular.jsが同じバージョン(たとえば、両方とも1.6.1)。

+0

それは私のために働く...ありがとう!! – Raj

+0

ようこそ);) – Karim

関連する問題