1

For example, I have such request to my server. As a response, I will get an array of objects with parameters: id, name and position. All of these loads into a table. How can I manipulate with an array $scope.employees if i will decide to change it later?

An answer from server is:

data = [{"id":1,"name":"Jack","position":"City guard"},{"id":2,"name":"Jim","position":"Sheriff"},{"id":4,"name":"Jack","position":"Cruel genius"},{"id":7,"name":"Guy","position":"Manager"}]  

And how to be sure, that request is already posted into a table, so i can perform some later operations?

angular
    .module('MyApp', [])
    .controller('MyController', ['$scope', '$http', MyController]);

function MyController ($scope, $http) {

    $http.get("/servlet").success(function(data){
        $scope.employees = data;
    });

}

function otherOperation () {
    $scope.employees.push({
        id : 5,
        name : "John",
        position : "Manager"
    });
}

HTML code:

<div id="content" ng-controller='MyController'>
                <table id="table">
                    <tr>
                        <th> ID </th>
                        <th> Name </th>
                        <th> Position </th>
                    </tr>
                    <tr ng-repeat="employee in employees">
                        <td>{{employee.id}}</td>
                        <td>{{employee.name}}</td>
                        <td>{{employee.position}}</td>
                    </tr>
                </table>
                <button ng-click="otherOperation"> Button </button>
</div>
2
  • If the method is a basic method "outside" angular then there is very little way apart from passing it the scope when calling that function. Method like that can often fall under an angular service, if you stay with angular for all it's a lot easier to access $scope and other angular bit (with $injector you can get them all) Commented Mar 29, 2015 at 11:14
  • I'm pretty new to angular and javascript, as I'm working with backend, so it will be much better, if you can show some code for my example. Commented Mar 29, 2015 at 11:24

1 Answer 1

1

The otherOperation method should be nested inside MyController, like this:

angular
.module('MyApp', [])
.controller('MyController', ['$scope', '$http', MyController]);

function MyController ($scope, $http) {

    function otherOperation () {
         $scope.employees.push({
             id : 5,
             name : "John",
             position : "Manager"
         });
     }  

     $http.get("/servlet").success(function(data){
        $scope.employees = data;
     });

}

You could also pass the $scope as a parameter, like this:

angular
.module('MyApp', [])
.controller('MyController', ['$scope', '$http', MyController]);

function MyController ($scope, $http) {

     $http.get("/servlet").success(function(data){
        $scope.employees = data;
     });

     otherOperation($scope);

}

function otherOperation ($scope) {
     $scope.employees.push({
        id : 5,
        name : "John",
        position : "Manager"
     });
 }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.