Reputation: 3331
I have a string that is stored in a JSON object which I'm getting from an API. I can't seem to get it to hide using ng-show
if there is no string to display.
In the actual controller, I get these results when trying various things:
string === null
gives me true
string.length
gives me Cannot read property 'length' of null
typeof string
gives me object
angular.equals({}, string)
gives me false
angular.isObject(string)
gives me false
null
I've tried everything I can think of, but I can't seem to get the ng-show
to understand when there is an empty string here. For your reference, when there is a value for string
it's just a plain and simple "string"
which is stored in the JSON.
So I just want to check if there is a string there really. Any ideas?
Sample JSON from console:
Object {
name: null
}
Upvotes: 1
Views: 1289
Reputation: 2412
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.stringData= "";
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<html>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<div ng-show = "stringData && stringData.length">{{stringData}}</div>
<input type="textbox" ng-model = "stringData">
</div>
</body>
</html>
Upvotes: 1
Reputation: 24541
Use double negation to convert it to boolean?
var isString = !!string;
This will convert string to true
when it exists or false
when it does not.
All you describe is correct. Type of null
is an object in JavaScript.
The simplest anyway is just to do the following:
ng-show="string"
or as I told before maybe more correct because you pass boolean
ng-show="!!string"
Upvotes: 2