Skip to content

Instantly share code, notes, and snippets.

@var-bin
Last active January 16, 2019 14:13
Show Gist options
  • Save var-bin/c0207c7ccd092943331e9fb1cd5d5071 to your computer and use it in GitHub Desktop.
Save var-bin/c0207c7ccd092943331e9fb1cd5d5071 to your computer and use it in GitHub Desktop.
lazyLoad modules in AngularJS
// home.module.js
"use strict";
import angular from "angular";
import { homeRouting } from "./home.module.routing";
import { BlogService } from "../blog/blog.service";
const HOME_MODULE = angular
.module("homeModule", [])
.service("BlogService", BlogService)
.config(homeRouting);
export { HOME_MODULE };
--- a/src/pages/home/home.module.js
+++ b/src/pages/home/home.module.js
@@ -5,8 +5,11 @@
import angular from "angular";
import { homeRouting } from "./home.module.routing";
+import { BlogService } from "../blog/blog.service";
+
const HOME_MODULE = angular
.module("homeModule", [])
+ .service("BlogService", BlogService)
.config(homeRouting);
export { HOME_MODULE };
// index.module.js
"use strict";
import angular from "angular";
class HomeIndexController {
constructor(BlogService) {
this.title = "HomeComponent";
this.BlogService = BlogService;
this.repos = [];
}
$onInit() {
this.getArticles();
}
getArticles() {
this.BlogService.getData()
.then((data) => {
this.repos = data;
});
}
}
HomeIndexController.$inject = ["BlogService"];
class HomeIndexComponent {
constructor() {
this.template = require("./index.view.html");
this.controller = HomeIndexController;
}
}
const HOME_INDEX_MODULE = angular
.module("home.module", [])
.component("homeComponent", new HomeIndexComponent());
export { HOME_INDEX_MODULE };
--- a/src/pages/home/index/index.module.js
+++ b/src/pages/home/index/index.module.js
@@ -5,11 +5,26 @@
import angular from "angular";
class HomeIndexController {
- constructor() {
+ constructor(BlogService) {
this.title = "HomeComponent";
+ this.BlogService = BlogService;
+ this.repos = [];
+ }
+
+ $onInit() {
+ this.getArticles();
+ }
+
+ getArticles() {
+ this.BlogService.getData()
+ .then((data) => {
+ this.repos = data;
+ });
}
}
+HomeIndexController.$inject = ["BlogService"];
+
class HomeIndexComponent {
constructor() {
this.template = require("./index.view.html");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment