could not update a variable in an angular service with ajax response data and use is to filter

Haitham Zoabi

i'm using the same code from this article (internationalization with angularjs) ... but i need the "tables" variable to be fetched from an ajax request response using "$http get" but couldn't !! ... here is the code,

var xlat = angular.module('xlat', []);

xlat.factory('xlatService', function ($http) {
    var currentLanguage = 'en';
    // var tables = $.extend(true, {}, initialXlatTables);
    var tables = {
        'en': {
            'textKeys.events': 'Events'
        }
    };

    var service = {
        getData: function () {
            var req = {
                method: 'GET',
                url: 'local/en_US.php',
                cache: true,
                headers: {
                    'Content-Type': 'json'
                }
            };

            $http(req).success(function (data) {
                tables = data;
            });
        },
        setCurrentLanguage: function (newCurrentLanguage) {
            currentLanguage = newCurrentLanguage;
        },
        getCurrentLanguage: function () {
            return currentLanguage;
        },
        xlat: function (label, parameters) {
            service.getData();     
            if (parameters === null || $.isEmptyObject(parameters)) {
                return tables[currentLanguage][label];
            } else {
                return $interpolate(tables[currentLanguage][label])(parameters);
            }
        }
    };

    return service;
});

but the variable "tables" does not change when i use the filter...

var xlat = angular.module('xlat', []);    
xlat.filter('xlat', ['xlatService', function (xlatService) {         
            return function (label, parameters) {
                return xlatService.xlat(label, parameters);
            };
        }]);
lujcon

Try this one:

var xlat = angular.module('xlat', []);    
xlat.filter('xlat', ['xlatService', function (xlatService) {         
    function myfiler(label, parameters) {
        return xlatService.xlat(label, parameters);
    };
    myfiler.$stateful = true;
    return myfilter;
}]);

Stateful filters - https://code.angularjs.org/1.3.9/docs/guide/filter

Secod: you should load tables in factory method not in xlat function.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

could not update a variable in an angular service with ajax response data and use is to filter

From Dev

Data object variable in angular service to update via ajax call

From Dev

Angular filter incoming service data

From Dev

Angular save response data into variable

From Dev

Update service data in Angular 4

From Dev

How to use a variable in Angular filter

From Dev

How to use a variable in Angular filter

From Dev

Using ajax json response in an angular scope variable

From Dev

Could I use excel to update data on a table?

From Dev

Pass Ajax variable value as response data name

From Dev

How do I set a variable within an Angular JS service to the response data from an $http call within an AngularJS service?

From Dev

Use Ajax data in PHP Variable

From Dev

Update angular service variable on window resize

From Dev

Update service variable with input form Angular?

From Dev

How to use ajax response JSON data?

From Dev

Filter api data in Angular 6 service

From Dev

How do I use the response in an ajax call to set a value in my jsx in react similar to filter/pipes in Angular 2?

From Dev

Share and update data using Angular service

From Dev

Angular custom directive use scope variable in filter

From Dev

Update ListView content via JSON response without the use of service

From Dev

Angular could not find service

From Dev

Update data from web service handling response delay and other errors

From Dev

Update data from web service handling response delay and other errors

From Dev

Angular filter doesn't update when service property changes

From Dev

file download is damaged when use observe: response in angular service

From Dev

How to get response of a service in an array for use, in angular 2

From Dev

Use variable to send data with jQuery AJAX

From Dev

Use jquery variable and loop in Ajax post data

From Dev

Angular not working in an ajax response

Related Related

  1. 1

    could not update a variable in an angular service with ajax response data and use is to filter

  2. 2

    Data object variable in angular service to update via ajax call

  3. 3

    Angular filter incoming service data

  4. 4

    Angular save response data into variable

  5. 5

    Update service data in Angular 4

  6. 6

    How to use a variable in Angular filter

  7. 7

    How to use a variable in Angular filter

  8. 8

    Using ajax json response in an angular scope variable

  9. 9

    Could I use excel to update data on a table?

  10. 10

    Pass Ajax variable value as response data name

  11. 11

    How do I set a variable within an Angular JS service to the response data from an $http call within an AngularJS service?

  12. 12

    Use Ajax data in PHP Variable

  13. 13

    Update angular service variable on window resize

  14. 14

    Update service variable with input form Angular?

  15. 15

    How to use ajax response JSON data?

  16. 16

    Filter api data in Angular 6 service

  17. 17

    How do I use the response in an ajax call to set a value in my jsx in react similar to filter/pipes in Angular 2?

  18. 18

    Share and update data using Angular service

  19. 19

    Angular custom directive use scope variable in filter

  20. 20

    Update ListView content via JSON response without the use of service

  21. 21

    Angular could not find service

  22. 22

    Update data from web service handling response delay and other errors

  23. 23

    Update data from web service handling response delay and other errors

  24. 24

    Angular filter doesn't update when service property changes

  25. 25

    file download is damaged when use observe: response in angular service

  26. 26

    How to get response of a service in an array for use, in angular 2

  27. 27

    Use variable to send data with jQuery AJAX

  28. 28

    Use jquery variable and loop in Ajax post data

  29. 29

    Angular not working in an ajax response

HotTag

Archive