0

I have done a service that gets a json file from the server with the translated values of the labels of my webapp. Seems to work fine:

mobilityApp.service('serveiTraduccions', function($resource) {

    this.getTranslation = function($scope) {
        var languageFilePath = 'traduccions/traduccio_en.json';
        $resource(languageFilePath).get(function (data) {                        
            $scope.translation = data;
        });
    };

});

What I am trying to do is acces that "$scope.translation" from my controler, I tried all and nothing worked. The object is saved in my $scope as you can see:

enter image description here

how can I get the values of the "registroBtnRegistro", "registroErrorRegistro" etc ?

Thanks in advance !

I tried:

  • console.log($scope.translation); -> undefined
  • console.log($scope['translation']); -> undefined
  • console.log($scope.translation.registroBtnRegistro); -> TypeError: Cannot read property 'registroBtnRegistro' of undefined console.log($scope.translation['registroBtnRegistro']); -> TypeError: Cannot read property 'registroBtnRegistro' of undefined
Cœur
  • 37,241
  • 25
  • 195
  • 267
Albert Prats
  • 786
  • 4
  • 15
  • 32
  • 1
    can you add the attempts to the question? maybe you're trying to access them before the promise is resolved? what's the markup like? – Eduard Gamonal May 05 '14 at 10:51

2 Answers2

1

Maybe you're trying to access these values from another $scope that not inherits the scope where you've created your translation model.

Try to assign this model directly to $rootScope, so you can access it from every scope:

mobilityApp.service('serveiTraduccions', function($resource, $rootScope) {

    this.getTranslation = function() {
        var languageFilePath = 'traduccions/traduccio_en.json';
        $resource(languageFilePath).get(function (data) {                        
            $rootScope.translation = data;
        });
    };

});
Alexandre TRINDADE
  • 917
  • 10
  • 21
  • keeps doing the same, it outputs undefined – Albert Prats May 05 '14 at 10:59
  • How are you trying to show these values in your html? I saw that your translation object is a promise, and not your data returned by your server. – Alexandre TRINDADE May 05 '14 at 11:02
  • ok, trying to output the values in my HTML like this works {{translation.registroPlaceholderRegistro}} I tried to do the console.log to check that the values were right, but if i can output it at the HTML is fine, thanks !! – Albert Prats May 05 '14 at 11:09
  • This is because $resource.get function is an asynchronous function, and you tried to call console.log BEFORE your resouce.get has returned. Try to call console.log like that: $timeout(function(){console.log($scope.translation.registroPlaceholderRegistro);}, 0); And don't forget to inject $timeout in your controller. – Alexandre TRINDADE May 05 '14 at 11:14
  • 1
    `$timeout` might be alright for quick debugging but it's a nasty solution for real code, which should use *promises* properly – Eduard Gamonal May 05 '14 at 11:24
  • @EduardGamonal, you're right, it was just to show that this code is working properly. – Alexandre TRINDADE May 05 '14 at 11:37
0

this answer is a blind attempt because your original post lacks basic information like the call from the controller. we can refine it until we make it work.

First, you should be returning something from your method:

mobilityApp.service('serveiTraduccions', function($resource) {
  this.getTranslation = function() {
    var languageFilePath = 'traduccions/traduccio_en.json';
    return $resource(languageFilePath);
  };
});

You are using $resource but you might as well use basic $http.get(). at least it doesn't look like a restful api to me. In any case, because it's an asynchronous request, it will not return the list of translated strings, but a resource "class" that allows methods like get, delete or the more general query():

from the docs: default methods are { 'get': {method:'GET'}, 'save': {method:'POST'}, 'query': {method:'GET', isArray:true}, 'remove': {method:'DELETE'}, 'delete': {method:'DELETE'} };

sidenote: injecting $scope in a service doesn't make much sense to me: services are used to encapsulate common logic accross components. However, you can pass a scope instance as a parameter.

Then, the controller that uses this should have the service injected and use a callback to get the results when they have arrived (asynchronous operation!):

TraduccioCtrl ... {
     $scope.translation = {}; // avoid undefined when the view just loads

     ServeiTraduccions.getTranslation.query(function (response) { 
        $scope.translation = response; // and angular's two-way data binding will probably do the rest
     });

}

The Angular docs about ng-resource have a working example. Other questions in SO have addressed this already too, like Using AngularJS $resource to get data

Community
  • 1
  • 1
Eduard Gamonal
  • 8,023
  • 5
  • 41
  • 46