3

So I have Googled this quite allot today, and I'm afraid the answers I got, did not satisfy the problem I'm facing. I'm trying to call a method within a controller (controller as syntax) by only using a string value.

I tried window['someFunctionName'](); as well as this['someFunctionName'](); and vm['someFunctionName'](), but non of them seems to work. I'm guessing it's because the function I'm calling is not in a global space, but rather local to the controller - example:

/* @ngInject */
function dashboardController(logger, _, moment) {
    var vm = this;

    vm.foo = foo

    function getSomeData() {
        // do something....
    }

    function foo() {
        window['getSomeData']();
        this['getSomeData']();
        vm['getSomeData']();
    }

}

It feels so trivial ... I could do this soooooo easily in C#, but struggling with something that feels so silly!

Any advice? Thanks!

3 Answers 3

1

If you don't want your function to be accessible from your template, you can still create a kind of container for your functions :

var functionContainer = {};
functionContainer.getSomeData = function() {
    // Do some stuff
}

Going further, your "get data" function should be in a service that you will inject into your controller.

angular.module('myApp').factory('getDataService', function(){
    return {
         'getSomeData': getSomeData,
         'getOtherData': getOtherData
    };


    function getSomeData() {
       // do some stuff
    }

    function getOtherData() {
       // do other stuff
    }
});

/* @ngInject */
function dashboardController(logger, _, moment, getDataService) {
    var vm = this;

    vm.foo = function () {
        getDataService['getSomeData']();
    };

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

6 Comments

Sure, but this will make the function public - this is what I ended up doing, but it feels like cheating
That's the way angular ^1.5 works with the 'controller as' syntax. If you don't want it to be "public", you can use the $scope way, but I wouldn't recomment it.
I totally agree
maybe I misunderstood your question. What if you bind the function to a new object? var functionContainer = {}; functionContainer.getSomeData = function(){};
@RichardBailey answer updated, I hope it'll suit you
|
0

Can you try

/* @ngInject */
function dashboardController(logger, _, moment) {
    var vm = this;

    vm.foo = foo

    vm.getSomeData = function() {
        // do something....
    }

    function foo() {
        vm.getSomeData();
    }

}

1 Comment

Thanks for the response! I see - almost like a get;set; - in this case only a get;
0

Just simply call getSomeData(); from foo. Like:

   function foo() {
       getSomeData();
    }

See this fiddle

3 Comments

Hmmm - sure that is pretty easy, but not my question. I'm trying to call a method using the string value and not the actual ref to the function.
okay, then you have to assign it to vm, like : vm.getSomeData = function() {//....); }, and then can call it vm['getSomeData']();
Thanks for the response!

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.