Javascript 如何检查angular js中是否存在对象?

Javascript 如何检查angular js中是否存在对象?,javascript,angularjs,Javascript,Angularjs,有没有办法知道一个对象是否存在于HTML中。我有一件像这样的东西 $scope.object = {name : 'usman', profession : 'developer'} 现在我想知道Html中的对象是否存在,或者对象是否只包含这样的文本 $scope.object = "Usman"; 现在有我的条件了 <div ng-if="object"> <p>Object exist and have attributes</p>

有没有办法知道一个对象是否存在于HTML中。我有一件像这样的东西

$scope.object = {name : 'usman', profession : 'developer'}
现在我想知道Html中的对象是否存在,或者对象是否只包含这样的文本

$scope.object = "Usman";
现在有我的条件了

    <div ng-if="object">
    <p>Object exist and have attributes</p>
    </div>

    <div ng-if="object">
    <p>object contain only string no attributes exist</p>
   </div>

对象存在并具有属性

对象仅包含字符串不存在属性

我想写一个这样的组合条件——如果对象没有属性,只包含字符串,那么显示div

给你

 <div ng-show="!objecthasnoattributes and objectOnlyContainsString">
<p>show this </p>
    </div>

展示这个


现在我该怎么做呢?

你可以这样写

<div ng-if="object !== undefined"> </div>
然后检查以下类型

    <div ng-if="object !== undefined && isObject(object)">
        <p>Object Value </p>
    </div>
    <div ng-if="myString !== undefined && isString(myString)">
        <p>String value</p>
    </div>

对象值

字符串值

询问更多问题


谢谢

使用一种方法,它可以更简短:

HTML:

<div ng-if="isObject()">
  <p>Object exist and have attributes</p>
</div>

<div ng-if="!isObject()">
  <p>object contain only string no attributes exist</p>
</div>
$scope.isObject = function() {
  return typeof($scope.object) === 'object';
}
<div ng-if="isString()">
  <p>Show this</p>
</div>
更新:

<div ng-if="isObject()">
  <p>Object exist and have attributes</p>
</div>

<div ng-if="!isObject()">
  <p>object contain only string no attributes exist</p>
</div>
$scope.isObject = function() {
  return typeof($scope.object) === 'object';
}
<div ng-if="isString()">
  <p>Show this</p>
</div>

如果只想显示字符串

$scope.isString = function() {
  return typeof($scope.object) === 'string';
}
那么您只需要一个html:

<div ng-if="isObject()">
  <p>Object exist and have attributes</p>
</div>

<div ng-if="!isObject()">
  <p>object contain only string no attributes exist</p>
</div>
$scope.isObject = function() {
  return typeof($scope.object) === 'object';
}
<div ng-if="isString()">
  <p>Show this</p>
</div>

展示这个


你可以做很多简单的事情

$scope.object = {name : 'usman', profession : 'developer'};

$scope.getBType = function(test){
  return( typeof test);
}

<div ng-if="getBType(object)=='object' && getBType(object.name)=='string'">
<p> Show this</p>
</div>
$scope.object={name:'usman',profession:'developer'};
$scope.getBType=函数(测试){
返回(测试类型);
}
展示这个


如果您确定输入的
对象将仅为以下类型

$scope.object = {name : 'usman', profession : 'developer'};

$scope.object = "Usman";
您可以简单地使用:

<div ng-if="object && object.name">
    <p>Object exist and have attributes</p>
</div>

<div ng-if="object && !object.name">
    <p>object contain only string no attributes exist</p>
</div>

对象存在并具有属性

对象仅包含字符串不存在属性


仅当对象存在且具有name属性时,才会满足条件#1。

请参阅更新的答案。问我更多的问题。很乐意帮忙。:)事实上,在html中,我希望条件放在一个地方,没有单独的字符。您希望只显示字符串吗?请参阅plunker和此条件ng show=“$parent.CustomPopusSelected&&$parent.CustomPopusSelected”使用它