微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我想用一些动态问题填充表单(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}}"]);
    };
}​

一切正常,除了模型是字面上的答案[“{{question.Name}}”],而不是评估的Answers [“GenderQuestion”]。如何动态设置模型名称

http://jsfiddle.net/DrQ77/

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐