如何基于angularjs中的输入参数动态创建$ scope

时间:2019-10-03 06:23:21

标签: html json angularjs

HTML

<div ng-repeat-start="param in parameters">
    <div ng-show="param.expensecolumn_type == tab.title">
        <div class="col-md-3" ng-show="param.inputtype == 'Textbox'"
             ng-repeat="para in param.columnsdata">
            <md-input-container ng-disabled="" class="md-block inputcontainer"
            >
                <label>{{para.ColumnName}}</label>
                <input ng-model="models[tab.title].values[para.ColumnName]" maxlength="16"/>
            </md-input-container>
        </div>
    </div>
</div>

<md-button ng-click="save_details(tab.title)"
           class="md-raised md-warn">Submit
    <md-tooltip>Submit</md-tooltip>
</md-button>

JS

$scope.models = {};
$scope.save_details = function(type){
    var str = "";
    var value = str.concat($scope,'.models.',type,'.values');
    $scope.data_todb = value;
    console.log($scope.data_todb)    
}

我的JSON数据-(现在$ scope.models在ng点击时看起来像这样)

  {
  "RENT": {
    "values": {
      "rent_qty": "34",
      "rent_amount": "100"
    }
  },
  "HOUSEHOLD": {
    "values": {
      "Account_no": "w",
      "name": "w"
    }
  }
}

预期输出JSON

{
  "rent_qty": "34",
  "rent_amount": "100"
}

我试图在调用ng-click指令时动态创建$ scope变量。$scope.save_details()函数中的变量(类型)可能会根据输入而变化。因此,我尝试根据变量创建动态范围。请先引导我找到解决方案,谢谢。

1 个答案:

答案 0 :(得分:0)

JS

 angular.forEach($scope.models, function(values, keys) {    
    if(type == keys){
      console.log(keys,values)
      for (o in values.values) {
        alert(o)
        alert(values.values[o])
      }
    }
 })

我无法创建动态范围,但是在上述情况下我得到了预期的输出。我觉得这可能会帮助某人