我有一些数据关注
app.controller('MailCtrls', function ($scope) {
$scope.employee = [{
id: 1,
name: 'Anil Singh1',
age: 30,
web: 'www.code-sample.com'
}, {
id: 2,
name: 'Sunil Singh2',
age: 25,
web: 'www.code-sample.com'
}, {
id: 3,
name: 'Sushil3',
age: 20,
web: 'www.code-sample.com'
}, {
id: 4,
name: 'Aradhya4',
age: 2,
web: 'www.code-sample.com'
}, {
id: 5,
name: 'Reena5',
age: 25,
web: 'www.code-sample.com'
}];
$scope.GetData = function () {
alert();
for (var i = 0; i <$scope.employee.length; i++) {
var abc = $scope.employee[i];
if (abc.name != null) {
$scope.emailNames = abc.name;
console.log($scope.emailNames);
}
在这里我需要Email=Anil Singh1,Sunil Singh2,Sushil3,Aradhya4,Reena5
格式的数据我如何才能以这种格式获取数据
但是我正在获取数据为:
Anil Singh1
Sunil Singh2
Sushil3
Aradhya4
Reena5
答案 0 :(得分:0)
您的代码中有一些小错误。
1. $scope.emailNames
的初始化丢失。
2. console.log
语句处于循环中。
3.您没有在字符串上附加+=
,而是覆盖了=
。
angular.module("myapp", []).controller('MailCtrls', function($scope) {
$scope.employee = [{
id: 1,
name: 'Anil Singh1',
age: 30,
web: 'www.code-sample.com'
}, {
id: 2,
name: 'Sunil Singh2',
age: 25,
web: 'www.code-sample.com'
}, {
id: 3,
name: 'Sushil3',
age: 20,
web: 'www.code-sample.com'
}, {
id: 4,
name: 'Aradhya4',
age: 2,
web: 'www.code-sample.com'
}, {
id: 5,
name: 'Reena5',
age: 25,
web: 'www.code-sample.com'
}];
$scope.GetData = function() {
$scope.emailNames = "Email=";
for (var i = 0; i < $scope.employee.length; i++) {
var abc = $scope.employee[i];
if (abc.name != null) {
$scope.emailNames += abc.name + ",";
}
}
console.log($scope.emailNames.substring(0,$scope.emailNames.length-1));
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp" ng-controller="MailCtrls" ng-init="GetData()">
</div>