質問

I want to use JSON instead of MongoLab example shown on AngularJS website. Here's the code from the website:

angular.module('mongolab', ['ngResource']).
factory('Project', function($resource) {
  var Project = $resource('https://api.mongolab.com/api/1/databases' +
      '/angularjs/collections/projects/:id',
      { apiKey: '4f847ad3e4b08a2eed5f3b54' }, {
        update: { method: 'PUT' }
      }
  );

Is there any way I could connect and put into JSON file available on my folder instead of hosting it to MongoLab? Any response would be really appreciated!

役に立ちましたか?

解決

You can look at this example:

Reading in JSON through Angular Resources Service

You want to return the Project variable from your factory

var Project = $resource('test.json', {} );
return Project;

then use it in the controller after you inject it:

Project.get(function(data){
  $scope.bar = data.foo;
});

Update: After looking thru the 1.1.3 angular-resource.js code, it looks like it always does an http request to get its data. Set up a simple RoR app with a REST interface for testing.

Or use plunker (like the linked question above) for complete remote testing.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top