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')
dev.meghraj
  • 8,542
  • 5
  • 38
  • 76

1 Answers1

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

Community
  • 1
  • 1
Jossef Harush Kadouri
  • 32,361
  • 10
  • 130
  • 129
  • You want to get the service before or after the app initialization? – runTarm Aug 02 '14 at 09:23
  • module.run() did that job for me from above answer but that way i can't block app to run.. – dev.meghraj Aug 02 '14 at 09:24
  • when app is bootstrapping can we have something like beforeRun() so i can check something and approve app to run – dev.meghraj Aug 02 '14 at 09:25
  • 1
    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. – runTarm Aug 02 '14 at 09:27
  • 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. – dev.meghraj Aug 02 '14 at 09:30
  • Is the `call service to check weather it should run or not` asynchronous? I mean it is an ajax request? – runTarm Aug 02 '14 at 09:34
  • ok. it's more clear now. there is `app.run` entry point where you could utilize for your needs http://nadeemkhedr.wordpress.com/2013/11/25/how-to-do-authorization-and-role-based-permissions-in-angularjs/ – Jossef Harush Kadouri Aug 02 '14 at 09:46
  • thanks buddy i got your point but for this i'll write my code in main app class, instead of isolating within service. – dev.meghraj Aug 02 '14 at 09:51
  • means i can't write $.get('/api/UserPermission', function(data) { in app.js – dev.meghraj Aug 02 '14 at 09:51
  • But this doesn't block the app from running. If you are ok with that, I'm confused of what you are trying to acheive in the first place. – runTarm Aug 02 '14 at 09:54
  • @runTarm, you are right. one way to block the app it's by url redirect. something like `$location.url('...');`. i will update the answer – Jossef Harush Kadouri Aug 02 '14 at 09:59
  • yes i'll try that too and now moved my ajax code in app.js just got inspired by http://stackoverflow.com/questions/19344214/problems-with-circular-dependency-and-oop-in-angularjs – dev.meghraj Aug 02 '14 at 10:25