Angular expression to check if the model is an array or object

How to check if an angular model is an array or an object? Is there a built-in or do I need to write a filter isArraywithArray.isArray()

{{[] | isArray}} does not work

+4
source share
2 answers

You can use the function angular.isArray. It is built inside Angularjs.

If you want to use this function inside your template, you need to create your own filter: http://docs.angularjs.org/tutorial/step_09

An example of what you want:

angular.module('...', []).filter('isArray', function() {
  return function (input) {
    return angular.isArray(input);
  };
});

Then you can use the filter inside your template:

{{ myVar | isArray }}
+11
source

I think you can also add underscore / lodash to rootScope and use it:

_ = require('lodash')

angular.module('app',[])
.run(function($rootScope){ 
   $rootScope._ = _
})

And in the template:

<div ng-show="$root._.isArray(foo)"> 
   <label> First element of Foo </label>
   <span> {{ $root._.first(foo) }} </span>
</div>

- lodash , . , , Math. javascript .

+1

Source: https://habr.com/ru/post/1531371/


All Articles