user3844782 user3844782 - 4 months ago 33
AngularJS Question

Using Angular $resource, Fetch data on button click not working

I am using angularjs ngresource to fetch a list of data from database on button click. But when i click button nothing happens. What i am doing wrong.


'use strict';
var App = angular.module('app',['ngResource']);


'use strict';
App.factory('Graph', ['$resource', function ($resource) {
return $resource(


'use strict';
App.controller('GraphController', ['$scope', 'Graph', function($scope, Graph) {
var self = this;
self.graph= new Graph();


self.fetchAllstats = function(){
self.graphs = Graph.query();


minimal html

<body ng-app="app" class="ng-cloak">
<div class="generic-container" ng-controller="GraphController as ctrl">
<div class="panel panel-default">
<input type="button" ng-click="fetchAllstats()" value="Fetch" class="btn btn-success">

<div class="panel-heading"><span class="lead">List</span></div>
<div class="tablecontainer">
<table class="table table-hover">
<th width="20%"></th>
<tr ng-repeat="g in ctrl.graphs">


You don't have a method named query in your factory,

Change your factory like this,

App.factory('Graph', ['$resource', function ($resource) {
    // Resource object 
    var resource = $resource('http://localhost:8080/SPsystem/stats');      
      /* Custom function to retrieve a issue list*/
    resource.query= function () {
        return this.query()      
    return resource;