gpt4 book ai didi

AngularJS:ng-repeat 中的 ng-model?

转载 作者:行者123 更新时间:2023-12-03 10:23:56 25 4
gpt4 key购买 nike

我正在尝试使用 ng-repeat 生成表单输入。
注意:'customFields' 是一个字段名称数组:["Age", "Weight", "Ethnicity"]。

 <div class="control-group" ng-repeat="field in customFields">
<label class="control-label">{{field}}</label>
<div class="controls">
<input type="text" ng-model="person.customfields.{{field}}" />
</div>
</div>

设置“ng-model”的最佳/正确方法是什么?我想把它作为 发送到服务器person.customfields.'fieldname' 其中 fieldname 来自“customFields 中的字段”。

最佳答案

<div ng-app ng-controller="Ctrl">
<div class="control-group" ng-repeat="field in customFields">
<label class="control-label">{{field}}</label>
<div class="controls">
<input type="text" ng-model="person.customfields[field]" />
</div>
</div>
<button ng-click="collectData()">Collect</button>
</div>

function Ctrl($scope) {
$scope.customFields = ["Age", "Weight", "Ethnicity"];
$scope.person = {
customfields: {
"Age": 0,
"Weight": 0,
"Ethnicity": 0
}
};

$scope.collectData = function () {
console.log($scope.person.customfields);
}
}

你可以试试 here .

更新:

对于验证,诀窍是输入 <ng-form>中继器内。请 try .

关于AngularJS:ng-repeat 中的 ng-model?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17865785/

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