ng-if输入条件

时间:2016-06-09 20:17:21

标签: javascript angularjs

我在ng-if的条件下有点混淆,需要一些帮助。我正在我的页面上使用帮助重复和自定义过滤器来呈现表单。检查此plunker。问题是我需要仅为数组中的三个键提供组合框,但占位符应该与此数据的当前值一致。 这是我的div

<div ng-repeat="k in rowKeys | filter: '!0' | filter: '!$$'" ng-model="rowVal" >
     <div ng-if="(k === 'id' || k.toLowerCase().endsWith('id') === false) ? true : false">
      <label for="rowValue[{{$index}}]" class="col-sm-2">
       {{k | hide:'.name' | makeUppercase }}:
       </label>
              <div class=" col-sm-2">
       <input ng-if="!isObject(rowData[k])" ng-disabled="disableInput(k)"  class="form-control rowValue" id="rowValue[{{$index}}]"
       ng-model="rowData[k]"/>

       <input ng-if="isObject(rowData[k])"
       ng-disabled="disableInput(k)"   class="form-control rowValue" id="rowValue[{{$index}}]"
       ng-model="rowData[k].name"/>
       <select ng-if="isObject(rowData[k]) && k == 'status'" ng-model="rowData[k].status" class="form-control">
          <option ng-repeat="item in status" value="{{item.value}}">{{item.label}}</option>
           </select>
        <select  ng-if="isObject(rowData[k]) && k == 'priority'" ng-model="rowData[k].priority" class="form-control">
         <option ng-repeat="item in priorities" value="{{item.value}}">{{item.label}}</option>
        </select>
       <select  ng-if="isObject(rowData[k]) && k == 'severity'" ng-model="rowData[k].severity" class="form-control">
      <option ng-repeat="item in severities" value="{{item.value}}">{{item.label}}</option>
      </select>
     </div>

如你所见,我有三个条件:

  1. 简单键 - 值
  2. 对象键 - 值
  3. 对象键 - 按键限制的值
  4. 所以问题是我无法编写强制组合框和输入字段正常工作的表达式。如果我正在做这样的事情ng-if="isObject(rowData[k]) && k == 'status'",输入就会以任何其他方式完全消失,我可以将它们一起看成一个键。第二个问题是我无法获得如何将当前值放入组合框中的占位符。如果有人能帮助我弄清楚我的错误在哪里,我感激不尽。

    代码:

      $scope.load = function(){
    
      $http({
           method: "GET",
           url: 'test.json'
           })
           .then(function success(response) {
                  $scope.rowData = response.data;
                      console.log($scope.rowData)
           }, function error(response) {
                     console.log("It has happend error in response")
      }).then(function(){
          $scope.id = $scope.rowData.id;
                console.log($scope.id);
               $scope.rowKeys = Object.keys($scope.rowData);
      })
      }
    
      $scope.isObject = function(value) {
        if(angular.isObject(value)) {
          return true;
        } else {
          return false;
        }
      }
    
      $scope.disableInput = function (obj) {
    
             if (obj === 'eventType'){
                    return true
                }
             else if (obj === 'id'){
                    return true
                }
             else if (obj === 'createdDate'){
                    return true
                }
             else if (obj === 'occuredDate'){
                    return true
                }
             else if (obj === 'study'){
                    return true
                }
      };
    
      $scope.priorities = [
                    { value: 'high', label: 'High' },
                    { value: 'medium', label: 'Medium'},
                    { value: 'low', label: 'Low'}
                ];
                $scope.severities = [
                    { value: 'benign', label: 'Benign' },
                    { value: 'severe', label: 'Severe'},
                    { value: 'medium', label: 'Medium' },
                    { value: 'mild', label: 'Mild'}
                ];
                $scope.status = [
                    { value: 'new', label: 'Open' },
                    { value: 'closed', label: 'Closed'}
                ];
    
    })
    

1 个答案:

答案 0 :(得分:0)

我自己打败了它。这是工作代码

<div ng-repeat="k in rowKeys | filter: '!0' | filter: '!$$'" ng-model="rowVal" >
    <div ng-if="(k === 'id' || k.toLowerCase().endsWith('id') === false) ? true : false">

        <label for="rowValue[{{$index}}]" class="col-sm-2">

            {{k | hide:'.name' | makeUppercase }}:
        </label>
        <div class=" col-sm-2">
            <input ng-if="!isObject(rowData[k])"
                   ng-disabled="disableInput(k)"
                   class="form-control rowValue"
                   id="rowValue[{{$index}}]"
                   ng-model="rowData[k]"/>

            <input ng-if="isObject(rowData[k]) && k !== 'status' && k !== 'priority' && k !== 'severity' "
                   ng-disabled="disableInput(k)"
                   class="form-control rowValue"
                   id="rowValue[{{$index}}]"
                   ng-model="rowData[k].name"/>

            <select ng-if="isObject(rowData[k]) && k == 'status'"
                    ng-model="rowData[k].name"
                    class="form-control rowValue"
                    id="rowValue[{{$index}}]"
                    ng-options='stat.value as stat.label for stat in status' >
                <option value=''>{{rowData[k].name}}</option>
            </select>
            <select ng-if="isObject(rowData[k]) && k == 'priority'"
                    ng-model="rowData[k].name"
                    class="form-control rowValue"
                    id="rowValue[{{$index}}]"
                    ng-options='prior.value as prior.label for prior in priorities' >
                <option value=''>{{rowData[k].name}}</option>
            </select>
            <select ng-if="isObject(rowData[k]) && k == 'severity'"
                    ng-model="rowData[k].name"
                    class="form-control rowValue"
                    id="rowValue[{{$index}}]"
                    ng-options='sev.value as sev.label for sev in severities' >
                <option value=''>{{rowData[k].name}}</option>
            </select>
        </div>
    </div>
</div>