0

How can we get data from loop of array like we get from ng-repeat use in our views?

I want this:

function test() {
   var data = [];
   for(var i = 0; i < 10; i++) {
     var newtest = {
                    text: 'text',
                    class: 'class_name',
                    user_chat_img: "images/noimage.jpg",
                    time_stamp: 'myJSON2'
                  }
                  // console.log('chat history',myJSON);
     data.push(newtest);
   }

   return data;
}

var $scope.message = test();
console.log('message', $scope.message);

Now i want to get this array values i.e text,class etc

this give me data in console like this

Array[1]
 0: Object
  class: "class_name"
  text: "text",
  time_stamp: "myJSON2",
  user_chat_img: "images/noimage.jpg"
  __proto__: Objectlength: 1__proto__: Array[0]

How can i get these values in my controller i will get these values in my views using ng-repeat like

ng-repeat={x in message}
and then 
x.class

thanks

1
  • I want to these array values in my controller not in my views I want just "How i get these arrays values in my controller " Commented Jan 18, 2017 at 14:43

1 Answer 1

0

Try this. Define your function outside controller. This will help you in accessing function as you mentioned.

ng-repeat="x in message"

Also I have made your function test as a scope function inside the controller.

<!DOCTYPE html>
<html ng-app="angularjs-starter">

<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<script>
    var app = angular.module('angularjs-starter', []);

    app.controller('MainCtrl', function ($scope) {
        $scope.message = test();
        console.log('message', $scope.message);
    });

    function test() {
        var data = [];
        for (var i = 0; i < 10; i++) {
            var newtest = {
                    text: 'text',
                    class: 'class_name',
                    user_chat_img: "images/noimage.jpg",
                    time_stamp: 'myJSON2'
                }
                // console.log('chat history',myJSON);
            data.push(newtest);
        }
        return data;
    }
</script>
</head>

<body ng-controller="MainCtrl">
<table>
    <tr ng-repeat="x in message">
        <td>{{x.class}}</td>
    </tr>
</table>
</body>

</html>

Sign up to request clarification or add additional context in comments.

6 Comments

this is good solution this is also working in my views but i want to get this in my controller to use this in another function
@NazarHussain I have updated the code. Define your function outside controller. This will help you in accessing function as you mentioned.
Thanks for your response but i think i can not convey my problem to you I want to use values that send me test function in my controller and then add some more message in that array and then i have to show this to my views
What is your requirement?
in this array i get message and then i send a call to api and that function give some old message Now i have to show all message i my view
|

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.