5

i have some module defined and services too with that module like below

var services=angular.module('app.services', []);
services.factory('ApiService', function($http,$cookies,UserService){

        var dataFactory = {};

        dataFactory.request=function(url,data,next){
            return "Hi";
        };

        return dataFactory;

});

now in another script i can access module like

services=angular.module('app.services')

but how can i get service instance from that module like

apiService=angular.module('app.services').service('ApiService')
2
  • What do you want to do with the instance? They are meant to be injected into controllers. Commented Aug 2, 2014 at 8:52
  • i want to access outside on app.init (my another fucntion) function (which i've created for starting app) Commented Aug 2, 2014 at 8:53

1 Answer 1

3

Edit:

after reading and understanding the author's comments, he was actually meant to block the entire app if the user is not permitted. his desired to do it by reusing the same code written in his ApiService factory.

--

You can 'hook' to app.run function which called before your controllers and you can utilize $window.location.href to relocate user to another page or site (if not permitted)

Iv'e updated this plunker with app.run entry

app.js

var app = angular.module('app', ['app.services']);

app.run(function(ApiService, $window) {
  result = ApiService.request();

  // This is where you check your permissions
  var has_permissions = false;
  // ...

  if (!has_permissions) {
    alert('being transferred to plnkr.co due to lack of permissions');
    $window.location.href = 'http://plnkr.co/';
  }

  // Otherwise, continue normally

});

Original:

i made this plunker

if you separate all logic to api.services module, include it in your app

app.js

var app = angular.module('app', ['app.services']);

then you could use it by referencing the desired factory - ApiService

app.controller('myCtrl', ['$scope', 'ApiService',
  function($scope, ApiService) {

    $scope.result = ApiService.request();

  }
]);

app.services.js

var services = angular.module('app.services', []);


services.factory('UserService', function() {

  var UserService = {};

  UserService.foo = function() {
    return "foo";
  };

  return UserService;

});


services.factory('ApiService', function($http, UserService) {

  var ApiService = {};

  ApiService.request = function(url, data, next) {
    return UserService.foo() + " Hi";
  };

  return ApiService;

});

plunker

enter image description here

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

12 Comments

You want to get the service before or after the app initialization?
module.run() did that job for me from above answer but that way i can't block app to run..
when app is bootstrapping can we have something like beforeRun() so i can check something and approve app to run
If you would like to decide whether to run the app or not, you could remove the ng-app and do the bootstrapping manually when desired.
i am doing so.. but that time i also need to call service to check weather it should run or not...i can start app but don't want to fire routing at least... thanks for all you doing.
|

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.