我有一个问题,当我使用angular.foreach时,我的全局变量不能在外面使用,但我认为在javascript中全局变量可以使用。有谁能给我解决方案?感谢
更新
这是我用xhr获取API API中数据对象的响应
HttpService("POST", url, param, function(response){
$scope.parsing = angular.fromJson(response.data);
$scope.result = {};
angular.forEach($scope.parsing, function(item){
$scope.result[item._id] = item;
$scope.getInclude = function(){
return 'templates/table.html';
};
});
});
这是我根据用户点击获取数据的HTML
<tbody ng-repeat="data in result">
<tr>
<td width="20%">
<button type="button" class="btn btn-primary" ng-click="getData(data)"><i class="fa fa-edit"></i> Edit</button>
<button type="button" class="btn btn-danger"><i class="fa fa-trash"></i> </td>
</tr>
</tbody>
这是来自按钮getData()的代码:
$scope.getData = function(data) {
var a;
angular.forEach(data.tags, function(item){
a = item.label;
});
console.log(a);
};
这是我的数据对象
{
"_id": "58942caba6551fd2c334741c",
"parent_id": "58942caba6551fd2c33473d1",
"level": 1,
"parent_source": 2,
"title": "title test thread ke8",
"url": "title-test-thread-ke8_yesp0y",
"description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.8",
"editor_pick": 0,
"up_vote": 1669,
"down_vote": 103,
"category_id": "58942caba6551fd2c3347372",
"user_id": "58942caba6551fd2c334737a",
"active": 1,
"status": 1,
"responded": "2017-02-03 14:09:31",
"revision": 0,
"extra": "",
"answer_count": 6,
"updated_at": "2017-02-03 14:10:26",
"created_at": "2017-02-03 14:09:31",
"tag_ids": [
"58942caba6551fd2c3347385",
"58942caba6551fd2c3347382",
"58942caba6551fd2c334737f",
"58942caba6551fd2c334737e",
"58942caba6551fd2c3347386",
"58942caba6551fd2c334737b",
"58942caba6551fd2c3347380"
],
"tags": [
{
"_id": "58942caba6551fd2c3347386",
"label": "Tag Name12",
"description": "Lorem ipsum dolor sit amet, consectetur adipisci12",
"url": "tag-name12",
"image": "http://placehold.it/300x30012",
"thread_count": 0,
"priority": 100,
"related": "[]",
"meta_title": "",
"meta_description": "",
"meta_keyword": "",
"active": 1,
"updated_at": "2017-02-03 14:09:31",
"created_at": "2017-02-03 14:09:31",
"thread_ids": [
"58942caba6551fd2c334738a",
"58942caba6551fd2c334738b",
"58942caba6551fd2c334738c",
"58942caba6551fd2c334738d",
"58942caba6551fd2c334738e",
"58942caba6551fd2c3347390",
"58942caba6551fd2c3347392",
"58942caba6551fd2c3347394",
"58942caba6551fd2c334739a",
"58942caba6551fd2c33473a3",
"58942caba6551fd2c33473a4",
"58942caba6551fd2c33473a6",
"58942caba6551fd2c334740f",
"58942caba6551fd2c3347411",
"58942caba6551fd2c3347412",
"58942caba6551fd2c334741b",
"58942caba6551fd2c334741c"
]
},
{
"_id": "58942caba6551fd2c334737e",
"label": "Tag Name4",
"description": "Lorem ipsum dolor sit amet, consectetur adipisci4",
"url": "tag-name4",
"image": "http://placehold.it/300x3004",
"thread_count": 0,
"priority": 100,
"related": "[]",
"meta_title": "",
"meta_description": "",
"meta_keyword": "",
"active": 1,
"updated_at": "2017-02-03 14:09:31",
"created_at": "2017-02-03 14:09:31",
"thread_ids": [
"58942caba6551fd2c3347388",
"58942caba6551fd2c3347389",
"58942caba6551fd2c334738b",
"58942caba6551fd2c334738c",
"58942caba6551fd2c3347391",
"58942caba6551fd2c3347392",
"58942caba6551fd2c3347393",
"58942caba6551fd2c3347398",
"58942caba6551fd2c334739a",
"58942caba6551fd2c3347412",
"58942caba6551fd2c3347414",
"58942caba6551fd2c334741c"
]
},
{
"_id": "58942caba6551fd2c3347382",
"label": "Tag Name8",
"description": "Lorem ipsum dolor sit amet, consectetur adipisci8",
"url": "tag-name8",
"image": "http://placehold.it/300x3008",
"thread_count": 0,
"priority": 100,
"related": "[]",
"meta_title": "",
"meta_description": "",
"meta_keyword": "",
"active": 1,
"updated_at": "2017-02-03 14:09:31",
"created_at": "2017-02-03 14:09:31",
"thread_ids": [
"58942caba6551fd2c3347387",
"58942caba6551fd2c3347390",
"58942caba6551fd2c3347393",
"58942caba6551fd2c3347397",
"58942caba6551fd2c3347398",
"58942caba6551fd2c334739c",
"58942caba6551fd2c33473a0",
"58942caba6551fd2c33473a3",
"58942caba6551fd2c33473a4",
"58942caba6551fd2c33473a6",
"58942caba6551fd2c33473ab",
"58942caba6551fd2c33473ac",
"58942caba6551fd2c334740d",
"58942caba6551fd2c3347410",
"58942caba6551fd2c3347411",
"58942caba6551fd2c3347412",
"58942caba6551fd2c3347416",
"58942caba6551fd2c334741c",
"58942caba6551fd2c334741d"
]
},
{
"_id": "58942caba6551fd2c3347385",
"label": "Tag Name11",
"description": "Lorem ipsum dolor sit amet, consectetur adipisci11",
"url": "tag-name11",
"image": "http://placehold.it/300x30011",
"thread_count": 0,
"priority": 100,
"related": "[]",
"meta_title": "",
"meta_description": "",
"meta_keyword": "",
"active": 1,
"updated_at": "2017-02-03 14:09:31",
"created_at": "2017-02-03 14:09:31",
"thread_ids": [
"58942caba6551fd2c334738b",
"58942caba6551fd2c334738e",
"58942caba6551fd2c3347390",
"58942caba6551fd2c3347396",
"58942caba6551fd2c3347399",
"58942caba6551fd2c334739a",
"58942caba6551fd2c334739f",
"58942caba6551fd2c33473a1",
"58942caba6551fd2c33473ac",
"58942caba6551fd2c334740f",
"58942caba6551fd2c3347414",
"58942caba6551fd2c3347416",
"58942caba6551fd2c3347418",
"58942caba6551fd2c334741a",
"58942caba6551fd2c334741c"
]
},
{
"_id": "58942caba6551fd2c334737b",
"label": "Tag Name1",
"description": "Lorem ipsum dolor sit amet, consectetur adipisci1",
"url": "tag-name1",
"image": "http://placehold.it/300x3001",
"thread_count": 0,
"priority": 100,
"related": "[]",
"meta_title": "",
"meta_description": "",
"meta_keyword": "",
"active": 1,
"updated_at": "2017-02-03 14:09:31",
"created_at": "2017-02-03 14:09:31",
"thread_ids": [
"58942caba6551fd2c3347388",
"58942caba6551fd2c334738b",
"58942caba6551fd2c3347394",
"58942caba6551fd2c3347398",
"58942caba6551fd2c334739c",
"58942caba6551fd2c334739d",
"58942caba6551fd2c33473a4",
"58942caba6551fd2c33473a5",
"58942caba6551fd2c33473a9",
"58942caba6551fd2c334740d",
"58942caba6551fd2c334740f",
"58942caba6551fd2c3347412",
"58942caba6551fd2c3347419",
"58942caba6551fd2c334741a",
"58942caba6551fd2c334741b",
"58942caba6551fd2c334741c"
]
},
{
"_id": "58942caba6551fd2c3347380",
"label": "Tag Name6",
"description": "Lorem ipsum dolor sit amet, consectetur adipisci6",
"url": "tag-name6",
"image": "http://placehold.it/300x3006",
"thread_count": 0,
"priority": 100,
"related": "[]",
"meta_title": "",
"meta_description": "",
"meta_keyword": "",
"active": 1,
"updated_at": "2017-02-03 14:09:31",
"created_at": "2017-02-03 14:09:31",
"thread_ids": [
"58942caba6551fd2c334738c",
"58942caba6551fd2c334738f",
"58942caba6551fd2c3347395",
"58942caba6551fd2c3347396",
"58942caba6551fd2c334739c",
"58942caba6551fd2c33473a4",
"58942caba6551fd2c33473a8",
"58942caba6551fd2c33473aa",
"58942caba6551fd2c33473ab",
"58942caba6551fd2c334740d",
"58942caba6551fd2c334740e",
"58942caba6551fd2c3347411",
"58942caba6551fd2c3347416",
"58942caba6551fd2c3347418",
"58942caba6551fd2c3347419",
"58942caba6551fd2c334741a",
"58942caba6551fd2c334741b",
"58942caba6551fd2c334741c"
]
},
{
"_id": "58942caba6551fd2c334737f",
"label": "Tag Name5",
"description": "Lorem ipsum dolor sit amet, consectetur adipisci5",
"url": "tag-name5",
"image": "http://placehold.it/300x3005",
"thread_count": 0,
"priority": 100,
"related": "[]",
"meta_title": "",
"meta_description": "",
"meta_keyword": "",
"active": 1,
"updated_at": "2017-02-03 14:09:31",
"created_at": "2017-02-03 14:09:31",
"thread_ids": [
"58942caba6551fd2c3347388",
"58942caba6551fd2c334738c",
"58942caba6551fd2c3347390",
"58942caba6551fd2c3347391",
"58942caba6551fd2c3347396",
"58942caba6551fd2c3347397",
"58942caba6551fd2c3347398",
"58942caba6551fd2c3347399",
"58942caba6551fd2c334739c",
"58942caba6551fd2c334739f",
"58942caba6551fd2c33473a0",
"58942caba6551fd2c33473a4",
"58942caba6551fd2c33473a9",
"58942caba6551fd2c33473aa",
"58942caba6551fd2c3347411",
"58942caba6551fd2c3347414",
"58942caba6551fd2c3347416",
"58942caba6551fd2c3347418",
"58942caba6551fd2c334741a",
"58942caba6551fd2c334741c",
"58942caba6551fd2c334741d"
]
}
],
"user": {
"_id": "58942caba6551fd2c334737a",
"status": "15",
"username": "usertest5",
"name": "User Test5",
"email": "test@test5",
"about": "Sebuah test user ke 5",
"image": "http://placehold.it/300x300",
"token": "$2y$10$lN7YtRg/j9wawJUm8XukkOBvWYMmhRX4E3p64ziQWbnhpm1HJFGd2",
"ref_id": "",
"login_ip": "",
"notif_check": "2017-02-03 14:09:31",
"updated_at": "2017-02-03 14:09:31",
"created_at": "2017-02-03 14:09:31"
},
"category": {
"_id": "58942caba6551fd2c3347372",
"label": "Sehat",
"active": 1,
"url": "sehat",
"parent_id": 0,
"level": 0,
"dfp_interest": "[]",
"meta_title": "",
"meta_description": "",
"meta_keyword": "",
"updated_at": "2017-02-03 14:09:31",
"created_at": "2017-02-03 14:09:31"
}
}
答案 0 :(得分:3)
它确实工作正常,这是你尝试实现的working fiddle。这是一个包含您对象的fiddle。
var myApp = angular.module('myApp',[]);
var a = [];
myApp.controller('MyCtrl', function ($scope) {
var data = [
{
label: 'test'
},
{
label: 'test2'
}
];
angular.forEach(data, function(item){
a.push(item.label);
});
console.log(a);
});