0

This is my first ever code written in angular after a 2-hour investigation and code play. Seems my knockout background doesn't help here. Please have a look at my code :

var dc = angular.module("DC", []);
dc.factory("incomeCategories",function($rootScope, $http) {
    var categories = {};

    $http.get("app/Categories/GetIncomeCategories").success(function(data) {
        categories = data;
    });
    return categories;
});
dc.controller("CategoriesController", function($scope, $http, incomeCategories) {
    $scope.incomeCategories = incomeCategories;
    $scope.incomeCategory = {};
});

Now here is my select option :

<select class="form-control" ng-model="incomeCategory" ng-options="ic.title for ic in incomeCategories"></select>

I think at the time of binding the incomeCategories is not loaded. But it should update the UI as it get populated right? like observables in knockout.

2
  • 1
    The problem (unverified) looks to be that you're returning categories as empty (call this object a), and when the AJAX call returns, you set categories to reference data (object b). That doesn't mean that object a is updated. Commented Mar 30, 2014 at 18:25
  • Thank you @JoachimIsaksson I removed the loading from service and put it inside of my controller then it worked as expected. I still haven't figured out why the previous one wasn't working though. Is this best practice to directly communicate with you back-end inside your controllers?
    – Milad
    Commented Mar 31, 2014 at 6:32

1 Answer 1

1

You might want to look at https://stackoverflow.com/a/18218579/56465 for a solution.

If you are using AngularJS 1.2 and want to use a simple

return $http.get("app/Categories/GetIncomeCategories")

in your factory, then you might want to turn on

$parseProvider.unwrapPromises(true)

as mentioned in the AngularJS 1.2 migration guide

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.