Noel Heesen
Noel Heesen

Reputation: 243

Angular: variable after $scope.?

This question might seem a bit strange. I'm trying to create functions to update the database via angular and because I'm lazy my function will be

gettable('tablebame')

It will select the table (MySQL) matching the parameter and return it. I didn't think about this issue untill I noticed it only worked with 1 table.

$scope.users = {};

var gettable = function(name) {

    httpFactory.setname(name);


    httpFactory.get(function(response) {

        $scope./* name inserted in function here */ = response;

    });

};

gettable("users");

I still had a static name where the comment is right now. I have tried things like but it doesn't work.

('$scope.' + name)

Is there any way to bind the return value 'response' to $scope. + 'name'?

Upvotes: 0

Views: 64

Answers (1)

ryanyuyu
ryanyuyu

Reputation: 6486

Just use javascript's bracket syntax to access the property with a variable name. Since $scope itself is just an object you can use

httpFactory.get(function(response) {

    $scope[name] = response;

});

Upvotes: 1

Related Questions