我有一個問題,當我使用angular.foreach時,我的全局變量不能在外部使用,但我認爲在javascript全局變量中可以使用。任何人都可以給我解決方案?由於全局變量不能在Angular.foreach中使用
UPDATE
這是我XHR來獲得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": "[email protected]",
"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"
}
}
什麼是全局變量?這與Angular有什麼關係?你使用的是什麼Angular版本?你有哪些代碼? –
@GünterZöchbauer'var a',它與javascript相關,因爲他在標籤中的作用範圍,版本在這裏並不重要,他在這裏「有」這個代碼。它的杯子問這種問題蛇。它的Stackoverflow的法律問題,我們都在某個時候開始。 – lin
@lin版本很重要,因爲這是angular1代碼。一些angular2用戶不知道angular1,因此標籤很混亂。 – echonax