如何使用AngularJS使用if声明条件和json响应?

时间:2015-03-25 17:42:09

标签: javascript json angularjs

我有Json响应,所以我想在窗体上实现条件,如果我得到三个字段的响应null,如editCtrl其他形式我想要隐藏。所以我在if语句中使用ng-show如果字段为null ng-show = false,但是它不起作用我仍然可以看到基于下面代码的表单。请更正我编码错误的内容。感谢

HTML

<form name="processRatingForm" novalidate ng-cloak
    ng-controller="EditProcessRatingCtrl" class="border-box-sizing">
    <div class="row">
        <div class="form-group col-md-6">
            <div>
                <label class="control-label" for="processInherentRisk">Process
                    Inherent risk rating</label>
            </div>
            <div class="radio-low" id="processInhRisk"
                ng-model-options="{ updateOn: 'blur' }" ng-model="processRating.inherentRiskRatingKey" >{{processRating.inherentRiskRatingKey}}</div>
        </div>
        <div class="form-group col-md-6">
            <div>
                <label class="control-label" for="finalOutcomeInherentRiskRatingKey">Process
                    Inherent risk Business Final</label>
            </div>
            <div class="radio-medium" id="irrFinalOutcome"
                ng-model-options="{ updateOn: 'blur' }"
                ng-model="processRating.finalOutcomeInherentRiskRatingKey">{{processRating.finalOutcomeInherentRiskRatingKey}}</div>
        </div>
    </div>
    <div class="row">
        <div class="form-group col-md-6">
            <div>
                <label class="control-label" for="controlEffect">Overall
                    Control Effectiveness Computed</label>
            </div>
            <div><select kendo-drop-down-list 
                    k-data-value-field="'id'"
                    k-data-text-field="'text'"
                    k-option-label="'Select'"
                    k-data-source="ctrlEffOptions"
                    ng-model-options="{ updateOn: 'blur' }"
                    ng-model="processRating.controlEffectivenessRatingComputeKey"></select> </div>
        </div>
        <div class="form-group col-md-6" ng-show="showEditdisForm">
            <div>
                <label class="control-label" for="controlEffBusiness">Overall
                    Control Effectiveness Business</label>
            </div>
            <div>
                <select kendo-drop-down-list 
                    k-data-value-field="'id'"
                    k-data-text-field="'text'"
                    k-option-label="'Select'"
                    k-data-source="ctrlEffOptions"
                    ng-model-options="{ updateOn: 'blur' }"
                    ng-model="processRating.controlEffectivenessRatingOverrideKey"></select>
            </div>
        </div>
    </div>
    <div ng-show="showEditdisForm">
    <div class="row">
        <div class="form-group col-md-6">
            <div>
                <label class="control-label" for="residualRatingText">Residual
                    Risk Computed</label>
            </div>
            <div>
                <select kendo-drop-down-list 
                    k-data-value-field="'id'"
                    k-data-text-field="'text'"
                    k-option-label="'Select'"
                    k-data-source="riskBusinessOptions"
                    ng-model-options="{ updateOn: 'blur' }"
                    ng-model="processRating.residualRiskRatingComputeKey"></select>
            </div>
            </div>
        <div class="form-group col-md-6">
            <div>
                <label class="control-label" for="residualRiskBus">Residual
                    Risk Business</label>
            </div>
            <div>
                <select kendo-drop-down-list 
                    k-data-value-field="'id'"
                    k-data-text-field="'text'"
                    k-option-label="'Select'" 
                    k-data-source="riskBusinessOptions"
                    ng-model-options="{ updateOn: 'blur' }"
                    ng-model="processRating.residualRiskRatingOverrideKey"></select>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="form-group col-md-6">
            <div>
                <label class="control-label" for="riskDirection">Risk
                    Direction</label>
            </div>
            <div>
                <select kendo-drop-down-list 
                    k-data-value-field = "'id'"                 
                    k-data-text-field="'text'"
                    k-option-label="'Select'"
                    k-data-source="riskDirOptions"
                    ng-model-options="{ updateOn: 'blur' }"
                    ng-model="processRating.residualRiskRatingOverrideKey"></select>
            </div>
        </div>
        <div class="form-group col-md-6">
            <div>
                <label class="control-label" for="riskAcceptance">Risk
                    Acceptance</label>
            </div>
            <div>
                <input type="radio" name="riskAcceptanceFlag" ng-model="processRating.riskAcceptanceFlag"
                    ng-value="'Yes'">&nbsp;Yes&nbsp;&nbsp;&nbsp; <input
                    type="radio" name="riskAcceptanceFlag" ng-model="processRating.riskAcceptanceFlag"
                    ng-value="'No'">&nbsp;No
            </div>
        </div>
    </div>
    <div class="row">
        <div class="form-group col-md-6">
            <div>
                <label class="control-label" for="riskAcceptanceComment">Rationale</label>
            </div>
            <div>
                <textarea class="form-control" ng-model="processRating.riskAcceptanceComment"
                    name="riskAcceptanceComment"></textarea>
            </div>
            </div>
            <div class="form-group col-md-6">
                <div>
                    <label class="control-label" for="mssControlFlag">Missing
                        Controls</label>
                </div>
                <div id="mssControlFlag"
                    ng-model-options="{ updateOn: 'blur' }"
                    ng-model="processRating.mssControlFlag">{{processRating.mssControlFlag}}</div>
            </div>
    </div>
</div>
    <div class="modal-footer">
        <button class="btn btn-default" ng-click="closeModal()">Cancel</button>
        <button type="submit" class="btn btn-primary"
            ng-click="saveProcessRating(); yesCallback()" ng-show="showEditdisForm">Save</button>
    </div>
</form>

MainCtrl.js

$scope.editProcessRtng = function (id) {

            $scope.selectedId = id;
            Rating.findProcessRating(id.processRatingKey).then(function (response){
              $scope.processRating = response.data;

              processRatingGridConfig.PrtmodalWinConfig.title = 'Edit Process Rating';
              $scope.$broadcast('editProcessRating',$scope.processRating);

            })
        };

EditCtrl.js

 $scope.$on('editProcessRating', function() {
          $scope.showEditdisForm = true;
        $scope.ProcessRatingWin.open().center();
        if($scope.processRating.inherentRiskRatingKey === 'null' || $scope.processRating.finalOutcomeInherentRiskRatingKey ==='null' 
          || $scope.processRating.controlEffectivenessRatingComputeKey ==='null') {
            $scope.showEditdisForm = false;
        } else {
          return true;
        }
      });

JSON.JS

{
    "processRatingKey": 154780,
    "processKey": 1000020,
    "beginTransactionTime": 1427203300000,
    "endTransactionTime": 1427203300000,
    "currentFlag": 1,
    "statusLookUpCode": "RS_ACTIVE",
    "createWorkerKey": 1000,
    "createdTimestamp": 1427097885000,
    "modifyWorkerKey": 1000,
    "modifyTimestamp": 1427203300000,
    "modifyUserText": " ",
    "sourceFeedKey": 1,
    "processRatingSessionKey": 1020,
    "inherentRiskRatingKey": 23,
    "controlEffectivenessRatingComputeKey": null,
    "controlEffectivenessRatingOverrideKey": null,
    "residualRiskRatingComputeKey": null,
    "residualRiskRatingOverrideKey": null,
    "overallControlEffectivenessOverrideText": null,
    "residualRatingText": null,
    "residualRiskDirKey": null,
    "riskAcceptanceFlag": null,
    "riskAcceptanceComment": null,
    "residualRiskComment": null,
    "mssControlFlag": null,
    "ratingStatus": "RA_RT_NON_EDITABLE",
    "finalOutcomeInherentRiskRatingKey": 23
}

3 个答案:

答案 0 :(得分:1)

 if($scope.processRating.inherentRiskRatingKey === 'null' || $scope.processRating.finalOutcomeInherentRiskRatingKey ==='null' 
      || $scope.processRating.controlEffectivenessRatingComputeKey ==='null') {
        $scope.showEditdisForm = false;
    }

应该是:

if(!$scope.processRating.inherentRiskRatingKey 
      || !$scope.processRating.finalOutcomeInherentRiskRatingKey 
      || !$scope.processRating.controlEffectivenessRatingComputeKey ) {
        $scope.showEditdisForm = false;
    }

答案 1 :(得分:-1)

而不是检查

$scope.processRating.inherentRiskRatingKey === 'null' 

尝试使用

$scope.processRating.inherentRiskRatingKey == null

答案 2 :(得分:-1)

您使用'null'作为字符串。你应该在没有引号的情况下这样做。此外,你可以和条件,而不是或。

if($scope.processRating.inherentRiskRatingKey === null && $scope.processRating.finalOutcomeInherentRiskRatingKey === null 
      && $scope.processRating.controlEffectivenessRatingComputeKey === null) {
        $scope.showEditdisForm = false;
    }