I am trying to update one of my scope properties dynamically
I have a scope property that looks like this
$scope.content.portrait_description.data
But the portrait_description needs to be able to be replaced by a variable called name, like this
$scope.content.name.data
and in this case, the name variable is equal to portrait_description.
When I try this it doesn't work, i've also tried $scope.content. + name + .data and $scope.content.{{name}}.data but neither work. Is there anyway to make this work?
asked Jul 25, 2014 at 20:09
Mitch
1,3945 gold badges21 silver badges39 bronze badges
2 Answers 2
Objects in Javascript works like array or dictionaries do this instead:
var varname = "somePropertyName";
$scope.content[varname].data = "hello";
answered Jul 25, 2014 at 20:12
Dalorzo
20k7 gold badges57 silver badges102 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Use bracket notation:
$scope.content[name].data
answered Jul 25, 2014 at 20:10
tymeJV
105k14 gold badges165 silver badges158 bronze badges
lang-js