如何在angularjs视图中创建动态变量

时间:2016-05-21 07:06:24

标签: angularjs variables dynamic

我想用角度来做这样的事情你怎么能这样做?

$scope.test = "foo"; 
$scope.foo = "bar";

并在视野中

<span>{{scope[test]}}</span> <!-- and return $scope.foo that is "bar" -->

1 个答案:

答案 0 :(得分:1)

应该用括号表示法:

questions": [
    {   
    "id" : "01-QuestionId",
    "type" : "multiple",
    "question" : "What is your Gender?",
    "options" : [
    {
            "key" : "a",
            "value" : "Male"
        },
        {
            "key" : "b",
            "value" : "Female"
        }
    ],
    "survey":{
    "id": "123",
    "name": "Test1",
    "description": "First Survey"
    }

    },
    {   
    "id" : "02-QuestionId",
    "type" : "multiple",
    "question" : "What is your income?",
    "options" : [   
        {
            "key" : "a",
            "value" : "1000"
        },
        {
            "key" : "b",
            "value" : "2000"
        }
    ],
    "survey":{
    "id": "123",
    "name": "Test1",
    "description": "First Survey"
    }

    }
]

<span>{{this[test]}}</span> 指向当前范围对象,因此对于变量this beeing&#34; foo&#34;,表达式将引用test