1.angular.forEach的用法
学到的知识,数组怎样遍历到页面上显示
item.name for item in data
首先data是一个数组
把对象转化为数组的形式
$scope.allDepartment = [];//数组
$scope.allUser = [];
$http.get("/attendRec/getDepAndUser")
.success(function (data) {
for(var key in data.departments){
$scope.allDepartment.push({
id:key,
name:data.departments[key]
})
}
for(var key in data.users){
$scope.allUser.push({
id:data.users[key].Id,
departmentId:data.users[key].departmentId,
name:key
})
}
})
.error(function () {
console.log("返回数据失败!");
});
$scope.allDepartment = [];
$scope.allUser = [];的数组如下:
在页面上进行遍历
所属部门: <select ng-model="leaveInfo.departmentId" ng-options="item.id as item.name for item in allDepartment" required>
2.数组的遍历问题2
终端设备中树控件设多选按钮
遍历出数组是这样的形式后
<tr ng-repeat="item in data">
data.name ..........
3.数组的遍历问题3
data= $scope.terminalist
for(var item in $scope.terminalist){
$scope.terminalist[item].choosed = oftenFuns.searchItem($scope.selectedDeviceList,$scope.terminalList[item].id);
}
刚开始的初始化:
打开页面有数据
通过判断长度
$http.get("longoDevice/getFirstChildren")
.success(function (data) {
$scope.deviceTree.allNodes = data;
$scope.deviceTree.allNodes.length ? $scope.deviceTree.allNodes[0].open = true : $scope.deviceTree.allNodes.open = true;
var regId = $scope.deviceTree.allNodes.length ? $scope.deviceTree.allNodes[0].id : $scope.deviceTree.allNodes.children[0].id;
loadTable(regId);
})
.error(function () {
console.log("返回数据失败!");
});
4.数组遍历问题4:
select 中ng-options的用法
<select style="color: #666666" ng-model="currentDevice.selectedGroup" ng-options="item.name for item in allDevice">
</select>
<select style="color: #666666" ng-model="currentDevice.id" ng-options="item.id as item.name for item in currentDevice.selectedGroup.children" ng-change="selectTerminal()">
$scope.allDeviced的数组形式
$http.get("/longoDevice/list")
.success(function (data) {
$scope.allDevice = data;
console.log($scope.allDevice)
if($stateParams.groupName)
for (var item in $scope.allDevice){
if($scope.allDevice[item].name == $stateParams.groupName) {
$scope.currentDevice.selectedGroup = $scope.allDevice[item];
}
}
else $scope.currentDevice.selectedGroup = $scope.allDevice[0];
$scope.currentDevice.id = $stateParams.deviceId ? $stateParams.deviceId : $scope.currentDevice.selectedGroup.children[0].id;
$scope.selectTerminal();
})
.error(function () {
console.log("返回数据失败!");
});
数组:
$scope.currentDevice.selectedGroup为
取出data的每一个下标,即6,5,4,。。1楼的数据
数组遍历的例子:
遍历数组
得到结果
还是数组遍历的问题:
这样的方式看看是否可以,看接口给的数据
$http.get(ApiAction.getComponentList(),{params:params})
.success(function(data){
var len=data.length;
for(var i=0;i<len;i++)
{
$scope.markers.push({
id: data[i].COMPONENT_ID,
lat: data[i].LATITUDE,
lon: data[i].LONGITUDE,
style: {
image: {
icon: {
src:"img/jk1.png",
scale:0.1,
anchor:[0.5,1]
}
}
},
info:data[i]
})
}
})
};
看借口的数据是怎么接到冰展示的
11,数组常用的方法总结
数组的方法:forEach,map,filter,reduce,some,every.sort
数组截取,
有一个问题对变量的起名要顾名思义起,从后台获取的results开始
数组的遍历问题的2中常用方法
$http.get("ziyuantongji.json")
.then(function (resourseData) {
$scope.dataTest = resourseData.data; //对象数组
angular.forEach(resourseData.data,function (data) {
});
//试试for的形式
$scope.str = [];
for(var item in $scope.dataTest){
$scope.str.push($scope.dataTest[item].nodeName)
console.log($scope.dataTest[item].nodeName);
}
})
数组清空的3中方式
var strZhuan = ["监测站","环保局","水文局","气象局","滇管局"];
//数组清空
strZhuan.splice(0,5);
//第一个参数为删除和添加数组的起始位置, //第二个参数为删除多少个元素, //第三个参数为添加新的元素
console.log(strZhuan = [])
console.log(strZhuan.length = 0)
console.log(strZhuan.splice(0,5))
数组字符串的相互转化
var b = strZhuan.join( ' ');//数组转字符串
console.log(getDataType(b))
var c = b.split(' ');//字符串转数组 分割 对象要 视情况,
console.log(getDataType(c))
数组的一些常用方法(增加删除,修改)
indexOf()方法的使用
console.log(strZhuan.valueOf())
//返回数组的原始值
console.log(strZhuan.indexOf("环保局"))
//用来判断数组是否包含某个元素项目,返回的是元素在数组中的序号
关于字符串截取的问题
网络地址
http://blog.csdn.net/kabulore/article/details/9355929
//字符串截取的方法测试
console.log(b)
console.log(b.substr(1,10));
console.log(b.substring(1,10));
对象转数组的形式:
var data={a:1,b:2,c:3}; //对象转为数组
function toArr(obj){
var arr1=[];
for(var i in obj){
arr1.push(obj[i]);
// console.log(obj[i]);
}
return arr1;
// arr1.fitter(function(x){console.log(arr1(x))});
}
数组转化为对象
var body = {};
ddd//数组
for (var i = 0; i < ddd.length; i++) {
body[ddd[i].name] = ddd[i].status;
}
关于数组的问题
有个接口是不知道这个接口当我每次根据id调用的话他有几个孩子
parant:{
parant:{
......
}
}
办法
$scope.titleTreesData = [];
function getTitleTrees() { //获取导航节点
api.getTitleTrees($stateParams.item2)
.then(function (res) {
// console.log(res.data);
var locationData = angular.copy(res.data);
while (!!locationData) {
var item = {
name: locationData.name,
id: locationData.id
};
$scope.titleTreesData.unshift(item); // 从数组开头插入
locationData = locationData.parent;
}
// console.log($scope.titleTreesData)
})
}