0

I'm making a mobile app using ionic (based on the AngularJS framework) where I want to display data from a RESTfull API. I'm pretty new to AngularJS so I'm not familiar with the best practices. I want to get data from this API but with a weeknumber parameters in the URL so I can get data from specific weeks. I have looked especially to the example of the AngularJS website.

This is what I have in my services:

var eventServices = angular.module('starter.services', ['ngResource']);

eventServices.factory('EventServiceAPI',['$resource', 
    function($resource) {
        return $resource('http://localhost:8080/schedulingAPI/plannedevents?weeknumber=:weeknumber', {}, {
            query: { method: 'GET', params:{weeknumber:'weeknumber'}, isArray: true} 
    });
}]);

This is what I have in my controller to get the API data:

$scope.events = EventServiceAPI.get({weeknumber: 7});

However, I still keep getting the error:

Error: [$resource:badcfg] object

When I use the full API URL in services and $scope.events = EventServiceAPI.query() in my controller, the full API-data is displayed without error.

I also don't get where to put the parameters; in the brackets after the resource URL or the params option in the query method.

Edit: The output of http://localhost:8080/schedulingAPI/plannedevents?weeknumber=:weeknumber

[{"id":2985,"event":{"eventId":589,"subject":"Masterproef","year":2014,"weekNumber":7,"dayNumber":6,"startHour":8,"startMinute":10,"endHour":12,"endMinute":45,"startTime":"2014-02-14T07:10:00Z","endTime":"2014-02-14T11:45:00Z","classgroups":[{"id":8,"name":"4ELICTI"},{"id":4,"name":"4ENAU"},{"id":10,"name":"4ELICTE"},{"id":1,"name":"4CHB"},{"id":3,"name":"4ENEL"},{"id":9,"name":"4EMEM"},{"id":2,"name":"4CHC"},[]],"teacher":null},"rooms":[{"id":24,"abbr":"D015"}]},{"id":4021,"event":{"eventId":604,"subject":"Bedrijfsbeleid 2 hc","year":2014,"weekNumber":7,"dayNumber":6,"startHour":8,"startMinute":10,"endHour":9,"endMinute":35,"startTime":"2014-02-14T07:10:00Z","endTime":"2014-02-14T08:35:00Z","classgroups":[{"id":6,"name":"4ELICT"},[]],"teacher":null},"rooms":[{"id":44,"abbr":"G120"}]}] 
3
  • I think you need to be using $scope.events = EventServiceAPI.query() instead of $scope.events = EventServiceAPI.get(). Can you post an example of what your data looks like inside http://localhost:8080/schedulingAPI/plannedevents?weeknumber=:weeknumber ? Commented Mar 15, 2014 at 12:23
  • But I need to pass the weeknumber parameter, is this possible with query()? Commented Mar 15, 2014 at 12:24
  • I just did something similar with Angular Treeview. I'm not sure if the same thing can be applied here, honestly, but here's what I did with my code. pastebin.com/6mqSfXDw Commented Mar 15, 2014 at 12:31

1 Answer 1

2

Replace params:{weeknumber:'weeknumber'} with params:{weeknumber:'@weeknumber'}, notice the @.

If the parameter value is prefixed with @ then the value of that parameter is extracted from the data object (useful for non-GET operations).

From angular documentation

And call your function with query:

$scope.events = EventServiceAPI.query({weeknumber: 7});

Actually, you can simplify your resource declaration like this:

eventServices.factory('EventServiceAPI',['$resource', 
    function($resource) {
        return $resource('http://localhost:8080/schedulingAPI/plannedevents');
    });
}]);

Angular will automatically append the query string for you

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.