Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set dynamic variable name in Angularjs controller

Tags:

angularjs

This is my controller code :

var selectableFields = ["customerName", "customerAddress"];

angular.forEach(selectableFields, function(field, key) {
var value = $.jStorage.get(field); //using jStore to get the values
$scope.field=value;

});

The objective is to be able to access {{customerName}} and {{customerAddress}} in the view. Can anyone please tell me what is the correct way of doing this?

like image 212
user727728 Avatar asked Jul 25 '14 14:07

user727728


1 Answers

Use $scope[field] = value; instead.

With $scope.field, you're creating a new attribute named field.

like image 117
sp00m Avatar answered Sep 23 '22 17:09

sp00m