0

mongolab の $resource を使用して角度のあるアプリにデータを表示しようとしています。モジュールの ngResource に依存関係を追加しました。それでも不明なプロバイダーと表示されます。ここで欠けている点は何ですか?

「$リソースが定義されていません」

注: 工場名が正しくない場合、エラーが発生していました: 不明なプロバイダー: employeesProvider <- employees

コード

<html>
<head>

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular-resource.js"></script>

<script type="text/javascript">

 //defining module
 var app = angular.module('myApp', ['ngResource']);

 //defining factory
 app.factory('employees', function () {

    return $resource('https://api.mlab.com/api/1/databases/humanresource/collections/Employees',
                    {apiKey: 'removedmykeyforpostinginSO'}
                  );
 });

 //defining controller
 app.controller('myController', function ($scope, employees) 
 {
    $scope.countries = employees.query();
 });  

</script>
</head>

<body ng-app="myApp">
     <div ng-controller="myController">
    <ul>
        <li ng-repeat = "objCountry in countries" >
            {{objCountry.name}}

        </li>
    </ul>
     </div>
</body>
</html>

Mongolab の JSON

[ { "_id" : { "$oid" : "57044f95e4b0427faa38585f"} , "name" : "Lijo" , "age" : "30"} ]
4

1 に答える 1

0

From AngulaJS Documentation, the recommended way of declaring factories is:

angular.module('myModule', [])
.factory('serviceId', ['depService', function(depService) {
  // ...
}])

I corrected my factory code as below, by injecting $resource:

app.factory('employees', function ($resource) {

    return $resource('https://api.mlab.com/api/1/databases/humanresource/collections/Employees',
                    {apiKey: 'jJbAltZ26vuIrgzUQetXh480JM0q9k6p'}
                  );
 });

References:

  1. $resource
  2. Tidy Up Your Angular Controllers with Factories and Services
  3. Angular: Passing data back to my controller from a factory ajax call
  4. Binding variables from Service/Factory to Controllers
  5. Angular passing data from factory to controller
于 2016-06-02T12:22:11.817 に答える