1

I have a problem, my global variable can't use on outside when i use angular.foreach, but i think in javascript global variable can use. Anybody can give me solution ? Thanks

UPDATE

This is my xhr to get response from data object in 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';
                };
            });
        });

This is my HTML to GET DATA BASED ON USER CLICK

<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>

This is my code from button getData() :

$scope.getData = function(data) {
    var a;

    angular.forEach(data.tags, function(item){     
        a = item.label;
    });

    console.log(a);
};

This is my data object

{
            "_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"
            }
        }
10
  • 4
    What global variable? How is that related to Angular? What Angular version are you using? Where do you have this code? Commented Feb 16, 2017 at 10:04
  • 1
    @GünterZöchbauer the var a, its related to javascript as he scoped in the tags, the version is not important here, he "have" this code here. Its mug to asking that kind of question snake. Its legal question for Stackoverflow and we all started at some point. Commented Feb 16, 2017 at 10:09
  • 1
    @echonax Take a look at the tags, no "Angular" tag over here, just "AngularJS". Its pretty clear what he is asking. Commented Feb 16, 2017 at 10:11
  • 1
    Angular2 tag was removed. Commented Feb 16, 2017 at 10:11
  • 2
    Could you please elaborate on what's not working? What is your expected output? This should work, but a will always have the last data.tag's value, as you overwrite it in each iteration of the forEach loop, and only log it to the console after that. Commented Feb 16, 2017 at 10:13

1 Answer 1

3

It does work fine, here is a working fiddle of what you try to achieve. Here is a fiddle including your object.

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);
});
Sign up to request clarification or add additional context in comments.

13 Comments

where I can put data.tags ? because from data.tags , my label can output value
actually , i edited your code "label : 'test'" to "label : data.tags" but still not working
@PriskilaNovia you could make it a global var if you need it in some other function inside your application or declaring it as a function var inside your controller. Its depending on where you need it. I updated my answer.
The output in console is [Object, Object, Object, Object, Object, Object, Object] even though the code a= item.label. Im confused
@PriskilaNovia please compare your data object structure with mine or update my fiddle to reproduce your problem. Please post that fiddle here in the comments.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.