我有一些数据如下
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
以这种格式 如何获取这种格式的数据 但我正在获取数据:
阿尼尔·辛格1
苏尼尔·辛格2
苏希尔3
阿拉迪亚4
里纳5
你的代码中有一些小错误。 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>