大家好我跳你帮我解决这个问题我有1个阵列我希望在我的index.html中用angularjs显示它但是它向我显示我的数组和数组中不存在的未定义元素如果有任何人可以帮助我如何删除index.html中的undefined元素 这是表(js)
var tab1 = ["E-Marketing & E-Commerce","Games","Topics","Security","Lunix","Electronics","XBOX","style"]
这是代码angularjs
this.selectedContinent;
this.selectedCountry;
this.selectedCity;
tab1.filter(function(i) {
return i != "b"
});
console.log(tab2);
// this is our dataset
this.continents = [
{
'id': 'AM',
'name': 'arabe',
'countries': [
{
'name': 'ar',
'cities':tab1
}
]
}
];
这是页面index.html
Continent:
<select id="continents" ng-model="ctrl.selectedContinent" ng-options="i.name for i in ctrl.continents">
<option value=''>Select</option>
</select>
<br/>
Country:
<select id="countries" ng-model="ctrl.selectedCountry" ng-options="i.name for i in ctrl.selectedContinent.countries">
<option value=''>Select</option>
</select>
<br/>
City:
<select id="cities" ng-model="ctrl.selectedCity" ng-options="i for i in ctrl.selectedCountry.cities" >
<option value='' ng-show="ctrl.selectedCity !='undefined'" ></option>
</select>
这是代码php
<?php
header('Access-Control-Allow-Origin: *');
include'../db.php';
$data=json_decode(file_get_contents("php://input"),1);
$getData = $connexion->prepare("SELECT * FROM table");
$getData->execute();
$getData2 = $getData->fetchAll();
if ($getData2) {
// get arabic categories
if ($getData2[0]['tra']==1) {
echo json_encode($getData2);
} if($getData2[0]['tra']==2){
// get english categories
echo json_encode($getData2 = $getData1);
}
}else{
echo "error";
}
这是控制器 所有代码angularjs
app.controller('ControlleArabe',['$scope','Upload','$timeout','$sce','$http','$routeParams','$location',function($scope,Upload,$timeout,$sce,$http,$routeParams,$location){
$scope.message = "hello word!";
var tab1 = [];
var tab2 = [];
$http({
method : "GET",
url : "php/select_ar_categories.php"
}).then(function (response) {
$scope.category = response.data;
for (let index = 0; index < $scope.category.length; index++) {
if ($scope.category[index]['tra']==1 && $scope.category[index]['name'] !== 'undefined') {
$scope.arabe = response.data[index]['name'];
tab1[index] = response.data[index]['name'];
}else if ($scope.category[index]['tra']==2 && $scope.category[index]['name'] !== 'undefined') {
$scope.arabe = response.data[index]['name'];
tab2[index] = response.data[index]['name'];
}
}
$scope.data = {
model: null
}
},function(response){
});
this.selectedContinent;
this.selectedCountry;
this.selectedCity;
// this is our dataset
this.continents = [
{
'id': 'AM',
'name': 'arabe',
'countries': [
{
'name': 'ar',
'cities':tab1
},
{
'name': 'Canada',
'cities':tab2
}
]
}
];
}]);
答案 0 :(得分:0)
我发现解决方案就是这个
Array.prototype.clean = function(deleteValue) {
for (var i = 0; i < this.length; i++) {
if (this[i] == deleteValue) {
this.splice(i, 1);
i--;
}
}
return this;
};
tab1.clean(undefined);
tab2.clean(undefined);
答案 1 :(得分:0)
也许更简单的解决方案而不是所有代码都使用过滤器。假设您的数组中包含未定义的内容:
var tab1 = ["E-Marketing & E-Commerce","Games","Topics","Security","Lunix","Electronics","XBOX","style",undefined];
我(ofc)添加了undefined。然后使用过滤器来摆脱它:
tab1.filter(Boolean);
这将产生正确(未定义)值的数组。