Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJs Inline Check if an array check

Inline in AngularJs is there a way to check if something is an array?

I would have thought this to work:

<div ng-show="Array.isArray(textStuff[0][1])">Hi</div>

I have verified it is in fact an array. Is there something I am missing or another way?

like image 732
Adam Avatar asked Aug 20 '14 17:08

Adam


People also ask

How to Check is array in AngularJS?

isArray() Function in AngularJS is used to return TRUE if the reference is an array and FALSE if it is not an array. Syntax: angular. isArray(value);

How do you check if an element is present in an array in angular?

The indexOf() method searches the array for the specified item, and returns its position. And return -1 if the item is not found.

How do you check if an array of objects contains a value in angular?

How do you check if an array contains an item? JavaScript Array includes() The includes() method returns true if an array contains a specified value. The includes() method returns false if the value is not found.

What is ng repeat end?

Overview. The ngRepeat directive instantiates a template once per item from a collection. Each template instance gets its own scope, where the given loop variable is set to the current collection item, and $index is set to the item index or key.


3 Answers

You can create global filters to use in your JS or HTML to check against object types. This way you don't pollute your $rootScope or $scopes to use it everywhere, unlike the accepted answer... Angular also has some built in utility functions that can check object types:

angular
    .module("yourModule")
    .filter("isArray", function() {
        return function(input) {
            return angular.isArray(input);
        };
    });

In HTML:

<div ng-show="{{ textStuff[0][1]) | isArray }}">Hi</div>

You may also inject the $filter service into your Controller to access the custom filter by name and compute the filtered results when your controller instance is instantiated (and also when your data changes). This prevents performance issues due to the view expression getting computed rapidly.

angular
    .module("yourModule")
    .controller("MyController", MyController);

MyController.$inject = ["$filter", "$scope"];

function MyController($filter, $scope) {

    this.testStuff = []; // your data
    this.filteredResult = $filter("isArray")(this.testStuff[0][1]);

    // or if you need to watch for data changes
    var vm = this;

    $scope.$watchCollection(
        function() { return vm.testStuff },
        function(newTestStuff) {  
            vm.filteredResult = $filter("isArray")(newTestStuff[0][1]);
        } 
    );
}

<div ng-controller="MyController as my">
    <div ng-show="my.filterResult">Hi</div>
</div>
like image 21
SirTophamHatt Avatar answered Oct 23 '22 21:10

SirTophamHatt


You can put angular.isArray on the scope...

$scope.isArray = angular.isArray;

<div ng-show="isArray(textStuff[0][1])">Hi</div>

Fiddle

like image 79
Anthony Chu Avatar answered Oct 23 '22 20:10

Anthony Chu


I would separate logic from the view. Add state in scope and then check it

 $scope.showHi = angular.isArray(textStuff[0][1]);

In view

<div ng-show="showHi">Hi</div>
like image 1
Lukasz Madon Avatar answered Oct 23 '22 21:10

Lukasz Madon