2017-06-21 18 views
0

以下は、角型アプリケーションの外観です。ページロードで角度サービスでページの読み込み時に関数を呼び出す方法

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

、家庭template.htmlはNG-ビューに挿入されています。

(のみのDataServiceの読み取りを呼び出すための正しい方法であるもの

以下

には何の手がかり)ページ 負荷の?

+0

[ページロードの呼び出し機能上のAngularjs]の可能な重複(https://stackoverflow.com/questions/27194805/angularjs-on-page-load-call-function) –

答えて

1

通常の使用例は、コントローラのコンストラクタでサービスを呼び出すことです。

John Papaのスタイルガイドには、このタイプのアーキテクチャとベストプラクティスに関する詳細なガイダンスが多数あります。私は非常にそれをお勧めしたいと思います。

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

関連する問題