Javascript 以角度将对象动态绑定到输入
我将使用这些对象保存表单中的数据,然后将其作为JSON发送到api:Javascript 以角度将对象动态绑定到输入,javascript,angularjs,javascript-objects,Javascript,Angularjs,Javascript Objects,我将使用这些对象保存表单中的数据,然后将其作为JSON发送到api: $scope.price = {} $scope.item = {"price":$scope.price, }; 我还有以下字段,用于在html页面上动态生成输入: $scope.fields = [ { name: $scope.item.title, title: 'Title', type: { view: 'input'
$scope.price = {}
$scope.item = {"price":$scope.price, };
我还有以下字段,用于在html页面上动态生成输入:
$scope.fields = [
{
name: $scope.item.title,
title: 'Title',
type: {
view: 'input'
}
},
{
name: $scope.price.regular,
title: 'Regualar Price',
type: {
view: 'input'
}
}
];
现在,为了生成表单,我使用以下代码:
<div class="form-group" ng-repeat="field in fields">
<label>{{ field.title }}:</label>
<span ng-switch on="field.type.view">
<span ng-switch-when="input">
<input
ng-model=field.name
type="text"
/>
</span>
</span>
</div>
但这将我限制在物体的一个层次。我希望能够绑定嵌套对象。而我似乎无法理解。谢谢大家! 已经有一个从JSON模式对象构建表单的指令;请参阅。不能简单地使用
ng repeat
生成字段。请看这篇文章,特别是这个答案:您需要使用$compile
插入带有指令的字段。
ng-model="item[field.name]"