2

Is there any way to know that an object exist or not in HTML. I have an object like this.

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

Now i want to know in Html if the object exist or not or does the object contain only a text like this .

$scope.object = "Usman";

Now there are my conditions.

    <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>

I want to write a combine condition like this - that if the object has no attributes and contains only string then show the div.

Here it is .

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

Now how can I do this ?

4 Answers 4

2

It can be more short with a single method:

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';
}

UPDATED:

If you want only string to be displayed,

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

Then you need only one html:

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

Here is a changed plunk

Sign up to request clarification or add additional context in comments.

3 Comments

actually in html I want the condition in one place no seperrate
you want only string to be displayed?
see the plunker plnkr.co/edit/XgEPKUL2Fm0I1dclytIV?p=preview and this condition ng-show="$parent.customPopupSelected && $parent.customPopupSelected" play with it
1

You can write as below.

<div ng-if="object !== undefined"> </div>

And to check type of var is OBJECT or STRING... you can do as follow.

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

    $scope.myString = "Usman";

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

Then check as below for types.

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

Ask for more queries

Thanks

1 Comment

See updated Answer. Ask me for more query. Happy to help. :)
1

You can make much easy way.

$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>

Comments

0

If you are certain that the input object will be of following types only.

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

$scope.object = "Usman";

The you can simply use:

<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>

Only if the object exists and has name property, the condition #1 will be met.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.