0

i have a service as below

module.service('translationService', [
    "$resource",
    function($resource) {
        var This = this;
        This.params = 'HELLO';
        This.getTranslation = function() {
            var languageFilePath = 'sample.json';
            //return languageFilePath;
            $resource(languageFilePath).get(function(data) {
                var temp = "";
                if (This.params != "") {
                    angular.forEach(data, function(key, value) {
                        if (value == This.params)
                            temp = key;
                    });
                } else {
                    This.translation = "Pls input key";
                }
                This.translation = temp;
                return temp;
            });
        }
    }
]);

In controller i am calling service,

 This.translate = translationService.getTranslation();

Problem is when i debug temp has value , but when i return value becomes null. May be its inside one more function .get() and return is losing scope. But if I return languageFilePath as commented above (//return languageFilePath;), value is passing to controller.

Please help me how to return value.

1
  • Can you please create a plunk for this case? Commented Feb 18, 2016 at 14:26

3 Answers 3

1

Convert your getTranslation() method to return a promise.

module.service('translationService', [ '$q', '$resource', translationService ]);

function translationService ($q, $resource) {
  var This = this;

  This.params = 'HELLO';

  This.getTranslation = getTranslation;

  function getTranslation () {
    var deferred = $q.defer(),
        languageFilePath = 'sample.json';

    $resource(languageFilePath)
      .get(_onGetTranslationSuccess, deferred.reject);

    function _onGetTranslationSuccess (data) {
      var translation;

      if (This.params === '') {
        deferred.reject('Pls input key');
      } else {
        angular.forEach(data, function (key, value) {
          if (value === This.params) {
            translation = key;
          }
        });

        if (angular.isDefined(translation)) {
          This.translation = translation;
          deferred.resolve(translation);
        } else {
          deferred.reject('Translation not found');
        }
      }
    }

    return deferred.promise;
  }
}

You can then consume the promise in your controller and get the translation.

translationService.getTranslation().then(function (translation) {
  This.translate = translation;
});
Sign up to request clarification or add additional context in comments.

7 Comments

You can just do $resource.get().$promise
how would you reject the promise in that case?
You can use it like normal promise with then. Promise.then(function() { // all loaded }, function() { // one or more failed }); or promise.reject() or promise.resolve()
i dont think you've thought what youre saying through. what would returning $resource.get().$promise do differently to what i've done? how would you implement rejecting the promise if the params is empty or the translation key is not found?
Maybe you're right, you're creating a promise, but not the one that you get from $resource. But you can get the response as a promise and then apply a filter, there are other ways of doing it. I wasn't saying that yours was bad,
|
0

You can handle a variable within the service and then declare a function to retrieve the value from the controler:

.service('translationService', function($q, $http, ...) {
  var result= '';

  ...

  function someFunction(...) {
    ...
    result='translation';
    ...
  }

  ...

  return {
    getTranslation: function() {return result;}
  };
})

On the controller you can do this:

var res = translationService.getTranslation();

Comments

-1

You have to return the value from your $resource function.

return $resource(languageFilePath)...

1 Comment

Maybe, but or he does it with a promise or it needs to return some value. To access in the controller

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.