只需要通过选择的"选项"数据

时间:2016-11-04 09:04:03

标签: javascript angularjs

以下是我的HTML, 我想将以下参数传递给数据库。我可以传递简单的文本值。但无法在选项标签中传递必需的参数。当我打电话给后调用时,在选择动态选项时,它会从该主机表发送主机的所有数据,但我只想从所选选项中传递主机名

<form name="main" novalidate>
  <div class="table-responsive" ng-show="visible8">
    <h4>Conditions</h4>
    <!-- <button type="button" class="btn btn-primary" ng-click="addCondition()">Apply</button> -->
    <button type="button" class="btn btn-primary" ng-click="addNewCondition()">Add Condition</button>
    <button type="button" class="btn btn-primary" ng-click="removeCondition()">Remove Condition</button>
    <button type="button" class="btn btn-primary" ng-click="cancel()">Cancel</button>
    <button class="btn btn-primary pull-right" ng-click="showAction()" ng-disabled!="">Next Step</button>                                
    <button class="btn btn-primary pull-right" ng-click="showBackRules()" ng-disabled!="">Back</button>                                
    <br>
    <br>
    <fieldset data-ng-repeat="choice in choices">
      <table class="table table-bordered" id="table" >
        <thead>
          <tr>
            <th>Parameter</th>
            <th>Value</th>
            <th>Comment</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <th>Select Condition Type</th>
            <td>
              <select name="option" class="form-control" ng-model="mySelect" ng-disabled="readonly" ng-change="selectedOption(mySelect);displayParam()">
                <option ng-repeat="type in model.condition_type" value= "{{ type.NAME }}" >{{ type.NAME }}</option>
              </select>
            </td>
            <td>Select Condition</td>
          </tr>
          <tr ng-repeat="child in model.rule_key[$index]">
            <!-- <td ng-repeat="child in parent_rule">
              {{ child.KEY_NAME }}
              </td> 

              </tr> -->
            <th> {{ child.KEY_NAME }} </th>
            <td>
              <input type="text" class="form-control" ng-model="rule1.conditions.Value"
                name="child.KEY_NAME"  ng-if="child.KEY_NAME != 'Host' && child.KEY_NAME != 'Sensor' && child.KEY_NAME != 'Field' && child.KEY_NAME != 'Operator'" required/>
              <span style="color:red" ng-show="main.AssetURL.$error.required ">Service name</span>
              <select name="child.KEY_NAME" class="form-control" 
                ng-options="host.Name for host in hosts track by host.id" 
                ng-model="rule1.conditions.Host" ng-if="child.KEY_NAME != 'Operator' && child.KEY_NAME != 'Sensor' && child.KEY_NAME != 'Field' && child.KEY_NAME != 'Value'" ng-disabled="readonly">
                <option value=""></option>
              </select>
              <select name="child.KEY_NAME" class="form-control" 
                ng-options="sensor.SnsName for sensor in model.sensors" 
                ng-model="rule1.conditions.Sensor" ng-if="child.KEY_NAME != 'Operator' && child.KEY_NAME != 'Host' && child.KEY_NAME != 'Field' && child.KEY_NAME != 'Value'" ng-disabled="readonly">
                <option value=""></option>
              </select>
              <select name="child.KEY_NAME" class="form-control" ng-model="rule1.conditions.Operator" 
                ng-if="child.KEY_NAME != 'Host' && child.KEY_NAME != 'Sensor' && child.KEY_NAME != 'Field' && child.KEY_NAME != 'Value'" required>
                <option value="greater">></option>
                <option value="smaller"><</option>
                <option value="equal">=</option>
              </select>
              <span style="color:red" ng-show="main.CaptLen.$error.required ">Select ServiceType</span>
              <select name="child.KEY_NAME" ng-if="child.KEY_NAME != 'Host' && child.KEY_NAME != 'Sensor' && child.KEY_NAME != 'Operator' && child.KEY_NAME != 'Value'" class="form-control" ng-model="rule1.conditions.Field" ng-disabled="readonly" >
                <option ng-repeat="field in ruleField[0].columns track by $index" value= "{{ field }}" >{{ field }}</option>
              </select>
            </td>
            <td> {{ child.DESCRIPTION }} </td>
          </tr>
        </tbody>
      </table>
    </fieldset>
  </div>
</form>

各自的JS

$scope.saveAll = function(rule1) {
    console.log(rule1);
    var newaction = JSON.stringify(rule1.actions);
    console.log("newaction", newaction);
    var newcondition = JSON.stringify(rule1.conditions);
    console.log("newcondition", newcondition);
    var newrule = {
        ruleName: rule1.ruleName,
        ruleDesc: rule1.ruleDesc,
        RULE_TYPE: rule1.RULE_TYPE,
        CONDITION_LIST: newcondition,
        ACTION_LIST: newaction,
    };
    console.log("newrule", newrule);

    $http.post("/nodejs/addRule", newrule)
        .success(function(data) {
            console.log("New rule", data);
            $scope.model.rule_list.push(data[0]);
            $scope.len = $scope.model.rule_list.length;
        })
        .error(function(err) {
            alert("error");
            //console.log(err);
        });
    // $scope.check();
    $scope.visible13 = false;
    $scope.visible2 = true;
    $scope.visible3 = true;
    $scope.visible7 = true;
    $scope.rule1 = {};
};

output Input screen

1 个答案:

答案 0 :(得分:0)

您应该使用ng-options语法将主机名绑定到ng-model,如下所示:

ng-options="host.Name as host.Name for host in hosts track by host.id"