2

I followed a lot of tutorials about this but can't have this working. I want to create an ordered list with a manipulated data received via API. I create a factory for each WebService I need to ask.

Factory:

angular.module('aparcare').factory('Items', function ($http) {
    return {
        get: function (fields) {
            return $http({
                method: "POST",
                url: "the-url-to-the-api",
                data: "field=2&anotherfield=4",
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            }).then(function (response) {
                return response.data.Items;
            }, function (error) {
                console.log(error);
            });
        }
    }
});

Controller:

angular.module('app').controller('ItemController', function ($http, $rootScope, $window, $scope, Items) {
    var controller = this;
    controller.items = [];
    //Call the API
    Items.get().then(function (response) {        
        //Transform array of objects in associative id => object
        angular.forEach(response, function (item) {
            controller.items[item.ID] = item;
        });

    }, function (error) {
        console.log("Error ocurred on API call");
    });
});

Template:

<ul class="dropdown-menu" role="menu" id="select-entity">

    <li ng-repeat="item in controller.items">
        <a ng-click="controller.select_item(item.ID)">{{ item.DESCRIPTION }}</a>
    </li>
</ul>

Currently the order of logs are 1 3 2 not 1 2 3

15
  • 1
    What exactly you mean by "can't have this working"?? do you get any error? Commented Jun 2, 2015 at 9:21
  • Problem with Angular (and most new frameworks) is that every example looks different. I always add the object to the $scope - $scope.items = []; What does your API return? Never had to loop through the items and create an array before - I just use the parsed JSON object. Commented Jun 2, 2015 at 9:24
  • How are you using controller in your view Commented Jun 2, 2015 at 9:25
  • @MehrdadKamelzadeh sorry, This works, no errors, but the array is not filling, if I put a console log before, inside and afther the "then", the order of logs are 1 3 2 not 1 2 3, sorry for my bad english Commented Jun 2, 2015 at 9:26
  • @LeeWillis Willis I need to create an array with the info, can't manage the current return without it Commented Jun 2, 2015 at 9:27

1 Answer 1

3

You don't need to create an associative array.

Items.get().then(function (items) { 
    //Assign items directly
    controller.items = items;
}, function (error) {

});

DEMO

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

2 Comments

Removing the angular.forEach and using your system to manage the items solved the problem and I'm sure this code is more optimized than mine! Thank you a lot
If your items array weren't sorted by your API, you could filter with angular like that: $filter('filter')(array, expression, comparator)

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.