AngularJs using instanceof in expression

Can i use typeof in angularjs?

I have an ngrepeat that processes my data and must check if the data is a string or an object.

<tr ng-repeat="text in data"> <td>{{angular.isObject(text) && 'IsObject'||text}}</td> </tr> 
+7
angularjs
source share
2 answers

Sounds good to use a filter:

 <tr ng-repeat="text in data"> <td>{{text|displayText}}</td> </tr> 
 angular.module('myApp').filter('displayText', function() { return function(text) { return angular.isObject(text) ? 'IsObject' : text; }; }); 
+4
source share

Please do not use a filter in this case, this is clearly the place for the function in your controller:

 <tr ng-repeat="text in data"> <td>{{isThisAnObject(text)}}</td> </tr> 

And in your controller:

 $scope.isThisAnObject = function(input) { return angular.isObject(input) ? 'IsObject' : input; }; 

This is not only less than code, but also in many other places. Filters are designed for a specific purpose. Not this!

+7
source share

All Articles