0

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?

2 Answers 2

3

Objects in Javascript works like array or dictionaries do this instead:

var varname = "somePropertyName";
$scope.content[varname].data = "hello";
Sign up to request clarification or add additional context in comments.

Comments

2

Use bracket notation:

$scope.content[name].data

2 Comments

@MitchGlenn -- You didnt use the code above, you have a . after content - remove it. It's NOT: $scope.content.[name].data = data.data;, it's : $scope.content[name].data
I just realized that! Thanks

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.