0

I am trying to update my view on button click, but I am doing something wrong. Console is showing that my array is just like I want it to be, but my ng-repeat list is not updating.

HTML:

<body ng-app="MyApp">
    <div layout="row" ng-controller="LekoviController">
        <md-content md-scroll-y flex class="lista_lekova_layout">
            <md-list>
                <md-list-item ng-repeat="lek in lekovi | filter:search">
                    <p ng-click="prikaz(lek)">{{lek.naziv}}</p>
                </md-list-item>
            </md-list>
            <button ng-click="loadMore()">Ucitaj jos</button>
        </md-content>
    </div>
</body>

JS:

var app = angular.module('MyApp', [
  'lbServices',
  'ui.router',
  'ngResource',
  'ngMaterial',
  'ngMdIcons',
  'ngRoute'
]);

app.controller('LekoviController', ['$scope', '$mdSidenav', 'Lekovi', 'ObliciLeka', function($scope, $mdSidenav, Lekovi, ObliciLeka) {
$scope.ucitano = false;
$scope.trenutniId = 0;
var lek = [];

$scope.lekovi = Lekovi.find({
    filter: {
        where: { lekId: {between: [1, 100]} }
    }
}, function(){
  $scope.ucitano = true;
});

$scope.loadMore = function() {
    $scope.trenutniId = $scope.lekovi.length + 100;
    lek = Lekovi.find({
        filter: {
            where: { lekId: {between: [$scope.lekovi.length, $scope.trenutniId]} }
        }
    }, function(){
        for(i = 0; i<lek.length; i++){
            $scope.lekovi.push(lek[i]);
        }
        console.log($scope.lekovi);
    });

};

I was trying to work with $scope.$apply() but that didn't work either.

neeev
  • 23
  • 8

1 Answers1

0

btw. where did you declare variable 'lek'? shouldn't it be used as a parameter of the success callback function?

$scope.loadMore = function() {
$scope.trenutniId = $scope.lekovi.length + 100;
Lekovi.find({
    filter: {
        where: { lekId: {between: [$scope.lekovi.length, $scope.trenutniId]} }
    }
}, function(lek){ // declare 'lek' as function parameter
    for(i = 0; i<lek.length; i++){
        $scope.lekovi.push(lek[i]);
    }
    console.log($scope.lekovi);
});
Michael
  • 3,085
  • 1
  • 17
  • 15