Search code examples
angularjsngtable

$scope variables vs var


in ngTable documentation, example 1 (http://bazalt-cms.com/ng-table/example/1)

There's this line:

<tr ng-repeat="user in $data">
    <td data-title="'Name'">{{user.name}}</td>
    <td data-title="'Age'">{{user.age}}</td>
</tr>

In the js file, he has

var data = [{name: "Moroni", age: 50},
            {name: "Tiancum", age: 43},
....
];

What specifically does $data do? I would normally do

$scope.data = [...];

and then

<tr ng-repeat="user in data">

Interestingly enough, when I create a second one, it doesn't work. e.g.

var data2 = [...];

and in HTML

{{$data2.length}}

This prints nothing. Can someone clarify this for me? Thanks!


Solution

  • In this example data is indeed only local and cannot be used directly. The example works because of the use of data in the getData function of the $scope.tableParams variable (code line 21):

    $scope.tableParams = new ngTableParams({
        page: 1,            // show first page
        count: 10           // count per page
    }, {
        total: data.length, // length of data
        getData: function($defer, params) {
            $defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
      // note the variable ^^^^
        }
    });
    

    More information about this configuration can be found in the documentation.