简体   繁体   中英

Call function from within angular factory

So i have an Angular factory with some functions. What i am trying to do is use myService to get the data and after its a success call another function that belongs to the factory:

myApp.factory('myFactory', function($http) {
    return {

        myFunction: function() {

            MyService.getData().then(
                function(response) {
                    var something = response;
                    this.anotherFunction(something); //returns undefined
                },

                function (error) {
                    //something terrible happened
                }
            );
        },


        anotherFunction: function(something) {
            console.log(something) //will not run
        }
     }
});

It fails because this.anotherFunction returns undefined

This is a scope issue - this does not refer to the function in the place you are using it. Try:

myApp.factory('myFactory', function($http) {
    var factory ={}

    factory.anotherFunction = function(something) {
        console.log(something) //will not run
    }

    factory.myFunction = function() {

        MyService.getData().then(
            function(response) {
                var something = response;
                    factory.anotherFunction(something); 
                },
                function (error) {
                    //something terrible happened
                }
            );
    }
    return factory;
});

Now we initialise factory as an object, assign functions to it, and return it. This means you should be able to reference factory.anotherFunction() from inside the first function. Although I've re-ordered them, as I'm not entirely sure if hoisting will apply in this method - but I could be wrong about that.

Except from the solution that @millerbr provided, which works perfectly fine i found that using a var that = this; variable solves the problem as well:

myApp.factory('myFactory', function($http) {
return {
    var that = this;

    myFunction: function() {

        MyService.getData().then(
            function(response) {
                var something = response;
                that.anotherFunction(something); //returns undefined
            },

            function (error) {
                //something terrible happened
            }
        );
    },


    anotherFunction: function(something) {
        console.log(something) //will not run
    }
 }

});

You must write it like this

   myApp.factory('myFactory', function($http) {
    var self{};
    self.anotherFunction = function(something) {
            console.log(something) //will not run
        } ;   
    self.myFunction = function() {
                MyService.getData().then(
                    function(response) {
                        var something = response;
                        self.anotherFunction(something); //returns undefined
                    },
                    function (error) {
                        //something terrible happened
                    }
               };
return self;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM