2

Just want to know your idea wht will be my approach on how to store external json data file into a services so that i can inject it into different controllers

Thanks

3 Answers 3

1

Try to use ngTranslation module. with this service you can store the static content(some json file, etc..) outside of the code, and not dirty your controllers/services...

EXAMPLE:
JS:

angular.module('app', ['ng-translation'])
  .config(['ngTranslationProvider', function(ngTranslationProvider) {
    ngStaticProvider
      .setDirectory('/assets/static')
      .setFilesSuffix('.json')
      .langsFiles({
        demo1: 'demo1',
        demo2: 'demo2'
      })
  }]);

//inject to controller
function PanelController($scope, translateFilter) {
  $scope.panelContent = translateFilter('key','fileName')
}

HTML:

<!-- use as a filter {{ key | translate: fileName  }}-->
<p>{{ key | translate: fileName }}</p>

<!-- use ng-translate directive, similar to ng-bind-->
<p ng-translate="file(key)"></p>
Sign up to request clarification or add additional context in comments.

Comments

0

Well, it all depends on the architecture of your application but I guess you could create a factory or service that will store data then simply inject that factory wherever needed.

If the data needs to be persistent, I would use something like localStorage. If it's a really small amount of data and your app cannot support HTML5, I would use cookies.

The more you elaborate the more accurate answer you will get.

Comments

0

Have look at the angularjs tutorial. It specifically address this issue. First go though http://docs.angularjs.org/tutorial/step_05 where you include the json in controller. Then follow http://docs.angularjs.org/tutorial/step_11 where it is moved to service.

$http.get() can be used to retrive data from REST urls as well.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.