Hi,

I'm beginner in angularJS,
I would like to know how to make an api call in a factory, 
I think the response is done to late, that's why _categories is undefined.
But I don't find any solution to fix it..

Thank you for your helps

Here is my code,


(function () {
    'use strict';

    angular
          .module('app.bc')
          .factory('dataContext', dataContext)
          .factory('breadCrumbSvc', breadCrumbSvc);


    function dataContext (api) {
      
        var _categories;

        api.index.list.get( {}, 
            // Success
            function (response)
            {
                _categories = response.data;
                alert(_categories); *//here _categories is defined*
            },
            // Error
            function (response)
            {
              console.error("erreur appel api");
                console.error(response);
            }
            );

        alert(_categories); *//here _categories is indefined*
 
        var businessCases = function () {
            return _businessCases;
        }
        var categories = function () {
            return _categories;

            

        }
        var addCategory = function (category) {
            _categories.push(category);
            return _categories;
        }
        var getRootCategory = function () {
            var rootItems = [];
            for (var i in _categories) {
                if (_categories[i].categoryId === "0") {
                   rootItems.push(_categories[i]);
                }
            }
            return rootItems;
        }
        var childCategories = function (parentId) {
            var items = [];
            for (var i in _categories) {
                if (_categories[i].categoryId === parentId) {
                    items.push(_categories[i]);
                }
            }
            return items;

        }

        return {
            fetchBusinessCases: businessCases,
            fetchCategories: categories,
            addCategory: addCategory,
            rootCategories: getRootCategory,
            childCategories: childCategories

        };
    }
    function breadCrumbSvc() {
        var crumbs = [];
        var init = function (crumb) {
            crumbs.push(crumb);
        }
        var get = function () {
            return crumbs;
        }
        var add = function (crumb) {
            crumbs.push(crumb);
            return crumbs;
        }
        var remove = function (index) {
          
            //var breadCrumb = crumbs[index];
            var range = crumbs.length - index;
            if (index + 1 < crumbs.length) {
                crumbs.splice(index + 1, range);
            }
            
            return crumbs;
         


        }
        return {
            get: get,
            add: add,
            remove: remove,
            init:init
        }
    }

})();

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to