Javascript 如何使用AngularJS将if语句条件与json响应一起使用?
我有Json响应,所以我想在表单上实现一个条件,若我在editCtrl中提到的三个字段的响应为null,我想隐藏表单的其余部分。因此,如果字段为空,我将使用ng show on if语句ng show=false,但它不起作用,我仍然可以看到基于以下代码的表单。请更正我编错的代码。谢谢 HTML EditCtrl.jsJavascript 如何使用AngularJS将if语句条件与json响应一起使用?,javascript,json,angularjs,Javascript,Json,Angularjs,我有Json响应,所以我想在表单上实现一个条件,若我在editCtrl中提到的三个字段的响应为null,我想隐藏表单的其余部分。因此,如果字段为空,我将使用ng show on if语句ng show=false,但它不起作用,我仍然可以看到基于以下代码的表单。请更正我编错的代码。谢谢 HTML EditCtrl.js $scope.$on('editProcessRating', function() { $scope.showEditdisForm = true;
$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
}
而不是检查
$scope.processRating.inherentRiskRatingKey === 'null'
尝试使用
$scope.processRating.inherentRiskRatingKey == null
应该是:
if(!$scope.processRating.inherentRiskRatingKey
|| !$scope.processRating.finalOutcomeInherentRiskRatingKey
|| !$scope.processRating.controlEffectivenessRatingComputeKey ) {
$scope.showEditdisForm = false;
}
您使用了
'null'
作为字符串。你应该不用引号。此外,您还可以选择和条件,而不是或
if($scope.processRating.inherentRiskRatingKey === null && $scope.processRating.finalOutcomeInherentRiskRatingKey === null
&& $scope.processRating.controlEffectivenessRatingComputeKey === null) {
$scope.showEditdisForm = false;
}
有一件事可能是编辑控制器中的$scope.processRating没有实例化为任何对象?至少传递给事件的数据未被使用。你在chrome控制台中是否有js错误?或者firebug?不,如果您可以添加一个plunkr/fiddle,我看不到控制台中有任何错误。如果您可以在这里提供帮助,我将不胜感激。如果您在第行设置断点:$scope.showEditdisForm=false;-它被击中了吗?我只是设置了一个断点$scope.showEditdisForm=false没有被调用我看到了问题!您正在检查多个表单上的
showEditdisForm
。这应该不是问题,我可以在任何适用的地方使用相同的属性。好的,一些神奇的事情发生了,它现在工作了。这里是一个新问题,如果其中三个字段中的任何字段获得值,它将显示表单,我想实现任何字段是否获得null ng show=“false”。。
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;
}
if($scope.processRating.inherentRiskRatingKey === null && $scope.processRating.finalOutcomeInherentRiskRatingKey === null
&& $scope.processRating.controlEffectivenessRatingComputeKey === null) {
$scope.showEditdisForm = false;
}