简体   繁体   English

如何在角度服务中的页面加载时调用函数

[英]how to call a function on page load in angular service

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 index.html

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

On page load, home-template.html is inserted into ng-view. 在页面加载时,将home-template.html插入到ng-view中。

No clue on below 下面没有任何线索

what would be the right way to call only dataService's read() on page load? 什么是在页面加载时仅调用dataService的read()的正确方法?

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. John Papa的风格指南中有很多关于此类架构和最佳实践的详细指南。 I'd highly recommend it. 我强烈推荐。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM