0

Below is how my angular app looks like.

(function () {
  "use strict";

angular
    .module("app.core", ['ngRoute']);

}());

Service

(function () {

'use strict';

angular
    .module('app.service')
    .factory('dataService', dataService);

dataService.$inject = ['$http', '$q'];

function dataService($http, $q) {

    var service = {
        create: create,
        read: read,
        update: update,
        remove: remove
    }

    return service;

    function read() {
        return
        $http.get("APIURL")
        .then(success)
        .catch(exception);

        function success(response) {

        }

        function exception(ex) {

        }

    }

  function create() {} 
  function update() {}
  function detete() {}

 }
})`

index.html

  <body ng-app="app.core">
     <div ng-view> </div>
  </div>

On page load, home-template.html is inserted into ng-view.

No clue on below

what would be the right way to call only dataService's read() on page load?

Kgn-web
  • 7,047
  • 24
  • 95
  • 161
  • Possible duplicate of [Angularjs on page load call function](https://stackoverflow.com/questions/27194805/angularjs-on-page-load-call-function) – Kyle Krzeski Jun 21 '17 at 19:10

1 Answers1

1

The normal use case is to call the service in the constructor of your controller.

John Papa's style guide has a lot of detailed guidance on this type of architecture and best practices. I'd highly recommend it.

https://github.com/johnpapa/angular-styleguide/tree/master/a1

Mike Feltman
  • 5,160
  • 1
  • 17
  • 38