【问题标题】:how to call a function on page load in angular service如何在角度服务中调用页面加载函数
【发布时间】:2017-06-21 17:51:13
【问题描述】:

下面是我的 Angular 应用的样子。

(function () {
  "use strict";

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

}());

服务

(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>

页面加载时,home-template.html 被插入到 ng-view 中。

下面没有线索

在页面上仅调用 dataService 的 read() 的正确方法是什么 加载?

【问题讨论】:

标签: javascript jquery html css angularjs


【解决方案1】:

正常的用例是在控制器的构造函数中调用服务。

John Papa 的风格指南有很多关于这种架构和最佳实践的详细指导。我强烈推荐它。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-04
    • 2011-04-20
    • 1970-01-01
    相关资源
    最近更新 更多