Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>When you use <code>factory</code> you have to return something. You're just defining a bunch of methods there but they aren't available to anybody.</p> <p>It's also good to use a different naming convention. For example, instead of LogsController, use LogsCtrl. AngularJS appends "Controller" internally and you might end up, in exotic situations, handling names like "LogsControllerController".</p> <p>A simplified <a href="http://jsfiddle.net/tCPG9/" rel="nofollow noreferrer">approach of using factory and returning the service</a>:</p> <pre class="lang-js prettyprint-override"><code>var ListLogsModule = angular.module("myApp", []); ListLogsModule.factory('ListLogsSrv', function ($http) { // first define the service (you're using a factory) var getData = function() { return "hey";//items; }; // then return it. // offer a public method "getData" that uses your internal getData() return { getData : getData } }); ListLogsModule.controller("ListLogsCtrl", function ($scope, ListLogsSrv) { $scope.w = "world"; $scope.logs = ListLogsSrv.getData(); }); </code></pre> <p>You also have an <code>$http</code> request in the factory. That means that you'll trigger the async request when you instantiate the service (when it is used for the first time), so nobody will wait for it to finish and you'll be getting <code>undefined</code>. If you are using this service in a controller, you will probably need to <code>resolve</code> a promise.</p> <p>An example of using a promise:</p> <pre class="lang-js prettyprint-override"><code>var promise = $q.defer(); var thisPageNumber = 1; ... var baseUrl = '../Api/LogApi/GetLogsByPage'; ... promise = $http.get(... </code></pre> <p>Now you can use this promise in the controller, for example, or in the methods of your service.</p> <p>I answered a related question two days ago <a href="https://stackoverflow.com/questions/17449383/angular-service-defination-server-or-factory/17450047#17450047">Angular Service Definition: service or factory</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload