1

I'm trying to build an angular module that uses a json file as a data source.

I am able to build using gulp a set of app.js / app.css files that can then be used by bower to create an installable module, I am stuck on how to include / reference this json file

so, I have a module defined as this (pseudo code)

(function() {
  'use strict';

  angular
    .module('myFoo', ['ngResource']);
    .service('myService',myService);

    /** @ngInject */
    function myService($resource) {
        return $resource('somefile.json');
    }
})();

I can now build this module, make it available via bower, then "bower install" this module, into my app, inject it and use it - unfortunately, I get a

https://myServer/somefile.json 404 (Not Found)

how do I embed / include this json file into a build that can be referenced this way ?

If I modify the service to return data directly, then it works as expected.

I was going to go down the route of gulp-injecting the contents into the service file directly if there is no better option.

Would appreciate any insights. Thanks!

1 Answer 1

1
angular.module('prerequisites', ['constants']).run(function ($http, $cacheFactory, somefileConstant) {
  $cacheFactory.get('$http').put('somefile.json', somefileConstant);
});

constants module and somefileConstant in particular can be built with gulp-ng-constant or gulp-ng-config. And prerequisites module can be replaced/redefined with empty module in the case when real json has to be requested.

angular
.module('myFoo', ['ngResource']);
.service('myService',myService);

/** @ngInject */
function myService($resource) {
    return $resource('somefile.json', null, { get: { cache: true } });
}

I guess that get action is meant for that, but can be replaced with any other action for this resource.

Sign up to request clarification or add additional context in comments.

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.