Question

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
Was it helpful?

Solution 2

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

OTHER TIPS

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;
        });
    };

});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top