我有一个简单的AngularJS动态表单,该表单由ng-model绑定到属性modelParams.value。每个表单字段都显示modelParams.value的值。但是,我希望有一个名为“ Default”的按钮,该按钮将所有表单字段的值设置为此关联数组中的其他某些属性,例如modelParams.defaultValue或modelParams。 oldValue。我假设一旦按下“默认”按钮,它将覆盖ng-model =“ modelParams.value”的值。

形式如下:

<form name="modelParamsForm">
    <div class="form-group" ng-repeat="modelParam in modelParams">
        <div class="row">
         //INPUT FORM FIELDS
         <input type="number" class="form-control input-sm" required ng-
         model="modelParam.value" >
        </div>
    </div>

    <button class="btn btn-primary btn-sm" ng-
        click="updateModelParams(modelParams, modelParamsForm)">
    </button>
    //DEFAULT BUTTON
    <button type="button" class="btn btn-default btn-sm" ng-
     click="default()">Default</button>
</form>


我的JSON看起来像这样:

[{"model":"MAF","paramname":"CascDefaultSpreadOverride","minvalue":"0","maxvalue":"100","description":"The defaault repo spread override to use for CASC positions.","defaultvalue":1.0,"value":1.0,"datatype":"FLOAT"},{"model":"MAF","paramname":"DefaultLotSize","minvalue":"1","maxvalue":"1000","description":"The minimum lot size that must be met for a collateral allocation.","defaultvalue":1.0,"value":1.0,"datatype":"INTEGER"},{"model":"MAF","paramname":"HtbColdHaircut","minvalue":"0","maxvalue":"100","description":"The haircut to apply to positions with a Cold HTB category.","defaultvalue":0.1,"value":0.1,"datatype":"FLOAT"},{"model":"MAF","paramname":"HtbExtraHotHaircut","minvalue":"0","maxvalue":"100","description":"The haircut to apply to positions with a Extra-Hot HTB category.","defaultvalue":0.9,"value":0.9,"datatype":"FLOAT"},]

最佳答案

$scope.field = 'value';

$scope.change = function() {
  $scope.field = 'oldValue';
}


...ng repeat blabla
<input ng-model="modelParam[field]"/>
...
<button ng-click="change()">Change</button>


编辑:这是一个有效的演示:http://jsfiddle.net/zy94an54/

10-06 04:50