简体   繁体   中英

AngularJS how to dynamically update part of scope with variable

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 $scope.content. + name + .data and $scope.content.{{name}}.data but neither work. Is there anyway to make this work?

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

var varname = "somePropertyName";
$scope.content[varname].data = "hello";

使用方括号表示法:

$scope.content[name].data

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM