Ada*_*dam 17 arrays angularjs ng-show
在AngularJs中内联是否有办法检查某些内容是否为数组?
我原以为这会起作用:
<div ng-show="Array.isArray(textStuff[0][1])">Hi</div>
Run Code Online (Sandbox Code Playgroud)
我已经证实它实际上是一个数组.有什么我缺少的或其他方式?
Ant*_*Chu 39
你可以放上angular.isArray范围......
$scope.isArray = angular.isArray;
Run Code Online (Sandbox Code Playgroud)
<div ng-show="isArray(textStuff[0][1])">Hi</div>
Run Code Online (Sandbox Code Playgroud)
您可以创建全局过滤器以在JS或HTML中使用以检查对象类型.这样你不会污染你的$ rootScope或$ scopes到处使用它,不像接受的答案...... Angular还有一些内置的实用程序函数可以检查对象类型:
angular
.module("yourModule")
.filter("isArray", function() {
return function(input) {
return angular.isArray(input);
};
});
Run Code Online (Sandbox Code Playgroud)
在HTML中:
<div ng-show="{{ textStuff[0][1]) | isArray }}">Hi</div>
Run Code Online (Sandbox Code Playgroud)
您还可以将$ filter服务注入Controller以按名称访问自定义筛选器,并在实例化控制器实例时(以及数据更改时)计算筛选结果.这可以防止由于视图表达式快速计算而导致的性能问题.
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>
Run Code Online (Sandbox Code Playgroud)