gpt4 book ai didi

javascript - 如何检查 Angular js中是否存在对象?

转载 作者:行者123 更新时间:2023-11-29 18:58:34 27 4
gpt4 key购买 nike

有没有办法知道一个对象在 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>

现在我该怎么做呢?

最佳答案

用一个方法可以更短:

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>

Controller :

$scope.isObject = function() {
return typeof($scope.object) === 'object';
}

更新:

如果只想显示字符串,

$scope.isString = function() {
return typeof($scope.object) === 'string';
}

那么你只需要一个html:

<div ng-if="isString()">
<p>Show this</p>
</div>

Here is a changed plunk

关于javascript - 如何检查 Angular js中是否存在对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48089431/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com