4

How can I refresh query result of $resource in AngularJS without refreshing the page?

My page:

<div class="container" ng-controller="AppController">
  <div ng-repeat="tile in items">
    <p>{{tile.name}}</p>
  </div>

  <button ng-click='??'> Reload tiles </button>

</div>

Controller:

(function(angular) {
    var AppController = function($scope, Item) {
        Item.query(function(response) {
            $scope.items = response ? response : [];
        });
    };

    AppController.$inject = ['$scope', 'Item'];
    angular.module("myApp.controllers").controller("AppController", AppController);
}(angular));

I know it's possibly very noob question however I could not find any solution yet (today is my first day with AngularJS, so any help is appreciated).

1 Answer 1

6

In your controller, create a function that is responsible for loading the data:

var AppController = function($scope, Item) {
    $scope.refresh = function(){
         Item.query(function(response) {
            $scope.items = response ? response : [];
        });
    };

    $scope.refresh();
};

In your button, just call the function:

<button ng-click='refresh()'> Reload tiles </button>
Sign up to request clarification or add additional context in comments.

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.