Skip to content

Angular

This document shows you how to create a Family Tree JS 2 Angular project.

  1. Create a new project:
bash
ng new familytree

Go to the project root folder:

bash
cd familytree
  1. Install Family Tree JS 2.
typescript
export default FamilyTree2;
  • Create a balkanapp folder in src/assets and copy familytree2.js and familytree2.d.ts into it.
  • Add the script in your angular.json file under options:
json
"scripts": [
  "src/assets/balkanapp/familytree2.js"
]
  1. In app.component.html add or replace the content with this:
html
<div id="tree"></div>
  1. In app.component.ts add this import:
typescript
import FamilyTree2 from '../assets/balkanapp/familytree2';

Then add this code in the exported class:

typescript
constructor() { }

ngOnInit() {
  const tree = document.getElementById('tree');
  if (tree) {
    const familyTree = new FamilyTree2(tree);
    familyTree.readOnly = false;
    familyTree.addFamilyMembers([
      { id: 1, spouseIds: [2], motherId: 3, fatherId: 4 },
      { id: 2 },
      { id: 3 },
      { id: 4 }
    ]).draw(1);
  }
}
  1. In styles.css add this:
css
html, body, #tree {
  height: 100%;
}
  1. Start the project:
bash
ng serve