3

I have a backend REST service that returns an array. I am trying to display the value returned by this REST service in one of my <select> using ng-options. However I get [object Object] on the lists.

Backend Java Rest service

@Path("/positions")
public class PositionRestService {

    ManagerService managerService = new ManagerService();   

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<String> getVacantPositions(@HeaderParam("iv-user") String uid) throws Exception{  
        Manager manager = managerService.getManager(uid);
        LdapSearch ldapProfile = new LdapSearch();
        List<String> vacantPositions = new ArrayList<String>();
        vacantPositions = ldapProfile.getChildPositions(manager.getPositionID());
        return vacantPositions;
    }
}

AngularJS Code - Service.js

services.factory('PositionsFactory', function ($resource) {
        return $resource('/userfeed/rest/positions', {}, {
            query: { method: 'GET', isArray: true}
        })

});

AngularJS - Controller.js

 app.controller('PositionCtrl', ['$scope', 'PositionsFactory', function ($scope, PositionsFactory) {    
    //$scope.positions = PositionsFactory.query();
    PositionsFactory.query({}, function (positionsFactory) {
        $scope.positions = positionsFactory;        
    })
}]);

My HTML code that uses the controller

 <select ng-model="myPosition" ng-options="position as position for position in positions"> </select>

Result I seee the follwing options on my list of options.

[object Object]

When I access the REST Service using my browser, I get the following array result which is what I am expecting on my option box above.

["CU13-00-00-004-","CU13-00-00-005-","CU13-00-00-006-"]

How can I get the values CU13-00-00-004-, CU13-00-00-005- and CU13-00-00-006- on my options using ng-options.

On using

{{positions|json}}
, I get following outcome:

[

{ "0": "C", "1": "U", "2": "1", "3": "3", "4": "-", "5": "0", "6": "0", "7": "-", "8": "0", "9": "0", "10": "-", "11": "0", "12": "0", "13": "4", "14": "-" }, { "0": "C", "1": "U", "2": "1", "3": "3", "4": "-", "5": "0", "6": "0", "7": "-", "8": "0", "9": "0", "10": "-", "11": "0", "12": "0", "13": "5", "14": "-" }, { "0": "C", "1": "U", "2": "1", "3": "3", "4": "-", "5": "0", "6": "0", "7": "-", "8": "0", "9": "0", "10": "-", "11": "0", "12": "0", "13": "6", "14": "-" } ]

Looks like it has converted the array into JSON object and created a place holder for earch character. The with ng-options show them as [object Object].

Please help. Any suggestion would be much appreciated.

4
  • Can you debug $scope.positions = positionsFactory; line and see the what $scope.positions is ? Commented Dec 5, 2013 at 4:46
  • 1
    in your controller, please post the output of: <pre>{{positions|json}}</pre>. It should show the real structure of positions Commented Dec 5, 2013 at 5:10
  • 1
    I don't think it's your issue, but if you're using the same value for the name and the value of the option, you don't need the <var> as <var>. It could just be: position for position in positions. Commented Dec 5, 2013 at 5:18
  • Hi @MaximShoustin I have updated the post, and included the <pre> result. Looks like it has converted my arrave value to each character and assigned it to the placeholder. Please have a look at the result posted above. Any comments? Commented Dec 5, 2013 at 23:01

0

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.