简体   繁体   中英

Braces in angular.js

Let's say for example, you have a .json file with "father": {"name":"Bob"} and a javascript file that gets the data from it and stores it in an array called family .

{{family.father.name}} would then print out the father's name on the html page.

Would it be possible to do something like:

In the javascript file:

$scope.member = "father";

In index.html:

{{family.{{member}}.name}}  
// Is there some way to replace father with a variable
// from the javascript file? Just curious.

Using dictionary notation, yes

var member = 'father'

family[member].name  === family.father.name

Or in angular

js:

$scope.member = 'father'
$scope.family = { 'father': { name : 'Bob' } }

html:

{{ family[member].name }}

Will output 'Bob'

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