gpt4 book ai didi

angularjs - 如何在 AngularJS 中设置动态模型名称?

转载 作者:行者123 更新时间:2023-12-02 02:34:01 25 4
gpt4 key购买 nike

我想用一些动态问题填充表单( fiddle here ):

<div ng-app ng-controller="QuestionController">
<ul ng-repeat="question in Questions">
<li>
<div>{{question.Text}}</div>
<select ng-model="Answers['{{question.Name}}']" ng-options="option for option in question.Options">
</select>
</li>
</ul>

<a ng-click="ShowAnswers()">Submit</a>
</div>

function QuestionController($scope) {
$scope.Answers = {};

$scope.Questions = [
{
"Text": "Gender?",
"Name": "GenderQuestion",
"Options": ["Male", "Female"]},
{
"Text": "Favorite color?",
"Name": "ColorQuestion",
"Options": ["Red", "Blue", "Green"]}
];

$scope.ShowAnswers = function()
{
alert($scope.Answers["GenderQuestion"]);
alert($scope.Answers["{{question.Name}}"]);
};
}​

一切正常,除了模型实际上是 Answers["{{question.Name}}"],而不是评估的 Answers["GenderQuestion"]。如何动态设置该模型名称?

最佳答案

http://jsfiddle.net/DrQ77/

您可以简单地将 JavaScript 表达式放入 ng-model 中。

关于angularjs - 如何在 AngularJS 中设置动态模型名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12553617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com