Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS pass a scope variable name as a function parameter

Hi I'm still new to AngularJs and was wondering if this was possible.

On my controller, I'm trying to create a function that takes a string parameter that will indicate which $http.get to call. I would then like to assign that parameter in my scope. For example

$scope.getpartial = function(partialtype) {
    var promise = "";
    switch(partialtype) {
        case "account":
             promise = $http.get("account url here");
             break;
        case "contact":
             promise = $http.get("contact url here");
             break;
    }
    promise.then(function(payload) {
        $scope.XXXXXXX = payload.data;
    });
}

Where XXXXXXX = partialtype == "account" or "contact"

so the result would be placed and stored under $scope.account and/or $scope.contact.

Is this possible or is there a better way to do this?

like image 637
Justin Avatar asked Dec 29 '14 22:12

Justin


1 Answers

Since $scope is just an object with properties, you can use bracket notation:

$scope[partialtype];
like image 119
tymeJV Avatar answered Oct 21 '22 02:10

tymeJV