Let's define simple boolean on the scope:
var mymodal = angular.module('mymodal', []);
mymodal.controller('MainCtrl', function ($scope) {
$scope.b = false;
});
How can I get type of a variable in the expression? typeOf
and Object.prototype.Tostring.call
don't work.
<div ng-controller="MainCtrl" class="container">
<div>
{{ b }}
{{ typeOf(b) }}
{{ Object.prototype.toString.call(b) }}
</div>
</div>
Here's JSFiddle: http://jsfiddle.net/g8Ld80x3/2/
i think the best way is to create a custom filter and use it as you wish within you expression, you can check this link that use for get the Object.keys of an object
for your case you can use
angular.module('customfilter', []).filter('typeof', function() {
return function(obj) {
return typeof obj
};
});
Just to show Zamboney's answer applied to my sample code:
Controller:
angular.module('customfilter', []).filter('getType', function() {
return function(obj) {
return typeof obj
};
});
var mymodal = angular.module('mymodal', ['customfilter']);
mymodal.controller('MainCtrl', function ($scope) {
$scope.b = false;
});
View:
<div ng-controller="MainCtrl" class="container">
<div>
{{ b }}
{{ b | getType }}
<div ng-if="(b | getType) == 'number'">
It's a number
</div>
<div ng-if="(b | getType) == 'boolean'">
It's a boolean
</div>
</div>
</div>
And fiddle: http://jsfiddle.net/g8Ld80x3/5/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With