Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot get angular.isString to work with ng-repeat

I'm trying to do an angular.isString comparison with ng-if inside an ng-repeat. But all items in the array are returned.

So I tried to just output the angular.isString result but it doesn't output anything.

Here is what I would like to do:

<li ng-repeat="item in data">
  <div ng-if="angular.isString(item)">
    {{ item }}
  </div>
</li>
function MyCtrl($scope)
{
    $scope.data =
    [
        "Hello",
        "-",
        123
    ];
}

Here's a fiddle: http://jsfiddle.net/m6k13whh/2/

like image 434
Adrian Rosca Avatar asked Nov 30 '25 12:11

Adrian Rosca


1 Answers

Angular expressions are evaluated against the scope. You can create a function which returns angular.isString:

<li ng-repeat="item in data">
  <div ng-if="isString(item)">
    {{ item }}
  </div>
</li>

$scope.isString = function(item) {
    return angular.isString(item);
}

You can also just filter all of the items with that function:

<li ng-repeat="item in data | filter:isString">        
   <div>
       {{ item }}     
   </div>
</li>
like image 98
eladcon Avatar answered Dec 02 '25 03:12

eladcon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!