2017-04-26 35 views
1

工作這是我的索引頁angular.isArray功能沒有在循環

<li class="active" ng-repeat="items in menuItems"> 
    <span ng-if="angular.isArray(items)"> 
     <a href="" class="act">{{items}}</a> 
    </span> 
</li> 

這是我的控制器

app.controller('mainController', function($scope) { 
    $scope.assests = 'include/assests.html' ; 
    $scope.menu = 'include/menu.html' ; 
    $scope.footer = 'include/footer.html' ; 
    $scope.menuItems = [ 'a' , ['xyz'] , ['abc'] ]; 
}); 

的MenuItems是數組和值的組合,但angular.isArray(項)既不評價真實也不虛假。

+0

http://stackoverflow.com/questions/25411291/angularjs-inline-check-if-an-array-check –

回答

3

<span ng-if="angular.isArray(items)">將在您的示波器中尋找名爲$scope.angular.isArray();那顯然不存在。

我建議你做到以下幾點:

<span ng-if="isArray(items)"> 

而在你的控制,定義此功能:

$scope.isArray = function(obj) { 
    return angular.isArray(obj); 
} 
+0

非常感謝你,它的工作很好! –

+1

你給了我完美的解決方案 –

0

只是把ng-if="isArray(items)",然後在你的控制器

$scope.isArray = function(value){ return Array.isArray(value) } 
0

還有其他解決方案,但我不會推薦它使用(最好有角度包裹。功能,並沒有直接從視圖角度訪問,但有時,它可能是有用的)。

您可以設置$scope.angular屬性與angular值是這樣的:

(function(angular) { 
'use strict'; 

    var app = angular.module('app', []); 

    app.controller('MainCtrl', ['$scope', function($scope) { 
     $scope.angular = angular; 
    }]); 
})(angular); 

在從這個個案訪問像angular.isArray將被轉化爲$scope.angular.isArray