Accessing a service from a controller in another file in AngularJS

heyred

I have a cross platform enterprise app built using Onsen UI and AngularJS.

The app has grown fast and so I have decided to split my main app.js file into separate files. Up to now each page has had its own controller and all controllers were in the one app.js file.

I also have a service defined in my app.js where I store values like username etc. for sharing between the controllers. I need to store these during the app cycle as all values entered on all the pages are sent to the server at the end of the user interaction.

I have successfully split the controllers into their separate files but I am having difficulty moving the services to its own file e.g. sharedProperties.js.

My "new" app.js (abbreviated) looks as follows:

// Create the module that deals with the controllers
var app = angular.module("myApp", ['onsen', 'loginController', 'registerController']);

Then I create each controller in its own file e.g. login.js as follows:

// Get the main app.js module
var login = angular.module("loginController", []);
login.controller("LoginController", function($scope, $http)
{
    // Need to get and set values from/to sharedProperties here
});

The different .js files are then declared in index.html and all this seems to be working OK.

I have tried to create the service sharedProperties in its own file e.g. sharedProperties.js in the same fashion as below, but this does not work.

// Get the main app.js module
var shared = angular.module("sharedProperties", []);
shared.service("SharedProperties", function()
{
    var username = "";   

    return {
        // Get and set the userName
        getUserName: function()
        {
            return userName;
        },
        setUserName: function(value)
        {
            userName = value;
        }
    }
});

I have tried adding the sharedProperties to my modules but neither worked. I have tried adding it to my main app as below:

// Create the module that deals with the controllers
var app = angular.module("myApp", ['onsen', 'sharedProperties', 'loginController', 'registerController']);

As well as to the function() of the individual controllers:

var login = angular.module("loginController", []);
login.controller("LoginController", function($scope, $http, sharedProperties)
{
    // Need to get and set values from/to sharedProperties here
});

But neither of those work. How do I make my sharedProperties from the new file available to all my controllers?

Also, how do I call my getter and setters then. When all controllers and service were in my original app.js file, I would create the controller, add sharedProperties to the function() and then call my getter and setters e.g.

app.controller("LoginController", function($scope, $http, sharedProperties)
{
    sharedProperties.setUserName(someValue);
    sharedProperties.getUserName();
});

Or is there a better way of doing this? As I mentioned, I have to split the app.js file into separate files as the app is growing quite large and is getting difficult to manage. And for reasons outside of my control the app will grow even bigger over time.

mwild

You have to inject the module 'sharedProperties' into the module you're looking to use it in.

By doing this it makes the module aware of everything inside 'sharedProperties'. So instead of injecting 'sharedProperties' into controllers and other services, you need to inject the actual service. like so:

    var app = angular.module("myApp", ['onsen', 'sharedProperties', 'loginController', 'registerController']);


    var login = angular.module("loginController", []);
    login.controller("LoginController", function($scope, $http, SharedProperties)
    {
    // Need to get and set values from/to sharedProperties here
    });

then from there, calling any functions you bound to that service is as simple as

SharedProperties.someFunction();
SharedProperties.someOtherFunction();

Another note: I would recommend long hand injection

login.controller("loginController", ['$scope','$http', 'SharedProperties', function($scope, $http, SharedProperties){
    //code in here
}]);

This is for if you are ever going to minify or uglify your code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Accessing a service from a controller in another file in AngularJS

From Dev

AngularJS - accessing elements inside a controller from a service

From Dev

angularjs - accessing a factory from a controller in another module

From Dev

Accessing $scope variable from another method in the same controller Angularjs

From Dev

SailsJS accessing a service from a controller

From Dev

SailsJS accessing a service from a controller

From Dev

AngularJS - Accessing var in a service which are defined in a controller

From Dev

Why data is not updated from a controller to another when set with service in AngularJS?

From Dev

AngularJS - Controller and Service in one File

From Dev

AngularJS - Controller and Service in one File

From Dev

angularjs - Accessing ui-bootstrap modal dismiss and close function from another controller

From Dev

Accessing a variable from another file

From Dev

Accessing a controller from inside a callback function in AngularJS

From Dev

AngularJS Accessing Aliased Controller Attribute From Directive

From Dev

Accessing a controller from inside a callback function in AngularJS

From Dev

Accessing data from one controller to another

From Dev

Accessing the variable from another model in my controller

From Dev

Accessing data from another controller in Rails

From Dev

AngularJS: accessing the ngTouch service from a directive?

From Dev

Gulp - Accessing gulpfile variable from AngularJS service

From Dev

[AngularJS][Ionic] Make Controller wait for Service that uses another $http service

From Dev

Accessing MainForm from another class file

From Dev

swift accessing variable from another file

From Dev

Problem accessing one jar file from another

From Dev

Accessing method from another cpp file

From Dev

Accessing a function variable from another file in python

From Dev

accessing atributes of a service in angularjs

From Dev

AngularJS - Calling a controller function from a service

From Dev

angularjs: returning json data from service to controller

Related Related

  1. 1

    Accessing a service from a controller in another file in AngularJS

  2. 2

    AngularJS - accessing elements inside a controller from a service

  3. 3

    angularjs - accessing a factory from a controller in another module

  4. 4

    Accessing $scope variable from another method in the same controller Angularjs

  5. 5

    SailsJS accessing a service from a controller

  6. 6

    SailsJS accessing a service from a controller

  7. 7

    AngularJS - Accessing var in a service which are defined in a controller

  8. 8

    Why data is not updated from a controller to another when set with service in AngularJS?

  9. 9

    AngularJS - Controller and Service in one File

  10. 10

    AngularJS - Controller and Service in one File

  11. 11

    angularjs - Accessing ui-bootstrap modal dismiss and close function from another controller

  12. 12

    Accessing a variable from another file

  13. 13

    Accessing a controller from inside a callback function in AngularJS

  14. 14

    AngularJS Accessing Aliased Controller Attribute From Directive

  15. 15

    Accessing a controller from inside a callback function in AngularJS

  16. 16

    Accessing data from one controller to another

  17. 17

    Accessing the variable from another model in my controller

  18. 18

    Accessing data from another controller in Rails

  19. 19

    AngularJS: accessing the ngTouch service from a directive?

  20. 20

    Gulp - Accessing gulpfile variable from AngularJS service

  21. 21

    [AngularJS][Ionic] Make Controller wait for Service that uses another $http service

  22. 22

    Accessing MainForm from another class file

  23. 23

    swift accessing variable from another file

  24. 24

    Problem accessing one jar file from another

  25. 25

    Accessing method from another cpp file

  26. 26

    Accessing a function variable from another file in python

  27. 27

    accessing atributes of a service in angularjs

  28. 28

    AngularJS - Calling a controller function from a service

  29. 29

    angularjs: returning json data from service to controller

HotTag

Archive