Javascript 如何检查条件属性是否有字符串?

Javascript 如何检查条件属性是否有字符串?,javascript,angularjs,Javascript,Angularjs,如何检查if条件if response有一个字符串,if response有一个字符串,然后执行if条件。基本上,如果服务器有错误,我想使$scope.ActiveFile为true main.js $scope.onError = function(e) { console.log('Error while uploading attachment', e.XMLHttpRequest.response); $scope.errorMessage = JS

如何检查if条件if response有一个字符串,if response有一个字符串,然后执行if条件。基本上,如果服务器有错误,我想使
$scope.ActiveFile
为true

main.js

  $scope.onError = function(e) {
        console.log('Error while uploading attachment', e.XMLHttpRequest.response);

        $scope.errorMessage = JSON.parse(e.XMLHttpRequest.response).techErrorMsg;
        if ($scope.errorMessage >= 1){
        $scope.applyActiveFile = true;
        }
    };
response.json

Server response: {"errorCode":500,"errorMsg":"Service failed. Please contact administrator.","techErrorMsg":"Sheet : PROCESS_INVENTORY not found in the File"}

要解决您的特定查询,这应该是可行的

if ($scope.errorMessage != null/blank) //whatever suits you
{
  $scope.applyActiveFile = true;
}
现在回答您的问题标题所说的-检查属性是否为字符串

if (typeof response === string)
/*typeof tells the type of operator, it will return number in case of number and  string in case of string*/
{
  $scope.applyActiveFile = true;
}
像这样的

  for (var i in $scope.errorMessages){
     if (typeof $scope.errorMessages[i] === "string"){
            alert($scope.errorMessages[i]);
     }
 }
要在浏览器控制台输入中进行测试,请执行以下操作:

var a = {"errorCode":500,"errorMsg":"Service failed. Please contact administrator.","techErrorMsg":"Sheet : PROCESS_INVENTORY not found in the File","thirdFieldNotString":1};
  for (var i in a){
     if (typeof a[i] === "string"){
        alert('Value is string');                
     }
  };

正如mic4ael所说,您可以使用一些条件,例如:

if ($scope.errorMessage)
    $scope.applyActiveFile = true;
if ((/^\s*$/).test($scope.errorMessage))
    $scope.applyActiveFile = false;
您可以使用一些正则表达式,例如:

if ($scope.errorMessage)
    $scope.applyActiveFile = true;
if ((/^\s*$/).test($scope.errorMessage))
    $scope.applyActiveFile = false;
…这将检查字符串是否为空或只有空格,并将触发器设置为false。您可能只想用它检查一个或两个值,因为否则性能会很高


许多其他解决方案…

如果($scope.errorMessage)
就足够了,因为这个变量不包含数字变量如果($scope.errorMessage==='string'){…}谢谢你的解释,没有问题。尽量保持简单,考虑性能:)谢谢你的答案有无数种方法可以实现这一点,这也会奏效谢谢你的答案!正如我在上面所说的,这种方法也会起作用。有很多方法可以解决这个问题