If you want to create an AEM app using Angular.js components and add them into a page, in this series, you
will learn how. Above is the vid and below you will find some useful notes.
In the second part we will see How to configure the Sling Model for the AEM Angularjs component.
- 1.
Pre-reqs
-
Have access to an Adobe Experience Manager instance. You will need aem 6.4 Service Pack 2 or newer.Have Maven installed, understand how it works and also understand how to use Adobe's archetype, you can watch my video about Adobe's archetype here: Creating an AEM Application using Adobe's archetype version 23 and avoiding typical issues
-
- 2.
Creating an AEM application with Angular.js support
-
You will need Adobe's archetype for creating your AEM application with SPA support. The support was added starting from the 23rd version of the archetype.
-
So let’s create the app from scratch. I am providing the most important properties in a single line here given that the other properties will be inferred from them, notice that the frontendModule property was set to angular
mvn -B archetype:generate ` -D archetypeGroupId=com.adobe.granite.archetypes ` -D archetypeArtifactId=aem-project-archetype ` -D archetypeVersion=23 ` -D aemVersion=6.5.0 ` -D appTitle="Angularjs Simple Example" ` -D appId="angularjs-simple-example" ` -D groupId="co.dlighthouse.aem.angularjs.simpleexample" ` -D frontendModule=angular ` -D includeExamples=y ` -D includeDispatcherConfig=n
-
Let’s build and deploy our new app (make sure that you have AEM up and running)
mvn clean install -PautoInstallPackage -Padobe-public
-
- 3.
AEM component using angularjs
-
For creating an AEM component using angular we need at least 4 things, a skeleton AEM component with a dialog, a angular component, an entry importing the component inside the app.module.ts file and a sling model which I’ll be configuring in the next video.
-
I am going to use the yeoman generator for creating the skeleton AEM component for me with some additional example fields in the dialog. I am calling it basic-component and I am making sure that the names for the folders and component group match my current application’s configuration.
yo aem-application:aem-component ` mavenAppsModuleName=ui.apps ` componentParentPath=angularjs-simple-example/components ` componentNodeName=basic-component ` componentName="Basic Component" ` componentGroup="Angularjs Simple Example - Content" ` mavenBundleModuleName=core ` javaRootPackageName=co.dlighthouse.aem.angularjs.simpleexample.core ` javaModelRelativePackageName=models ` javaModelClassName=BasicModel
-
We should end up with a component inside componentParentPath=angularjs/components called Basic Component, if you check the dialog’s xml file you will find two example fields, one for the text and another for the checkbox.
<items jcr:primaryType=\"nt:unstructured\"> <text jcr:primaryType=\"nt:unstructured\" sling:resourceType=\"granite/ui/components/coral/foundation/form/textfield\" fieldLabel=\"Text\" name=\"./text\"/> <checkbox jcr:primaryType=\"nt:unstructured\" sling:resourceType=\"granite/ui/components/coral/foundation/form/checkbox\" text=\"Checkbox\" uncheckedValue=\"false\" value=\"true\" name=\"./checkbox\"/> <checkboxType jcr:primaryType=\"nt:unstructured\" sling:resourceType=\"granite/ui/components/foundation/form/hidden\" name=\"./checkbox@TypeHint\" value=\"Boolean\"/> </items>
-
Before creating the component make sure that you have the Angular command-line interface (CLI) by first installing it in your computer
npm install -g @angular/cli
-
Use the Angular.js CLI to create the component inside the components folder.
In the ui.frontend module you will find the Angular.js components inside the src/app/components folder. Add the imports for the Input annotation object from the angular/core library and the MapTo helper from adobe.ng generate component components/basic-component
import { Component, Input, OnInit } from '@angular/core'; import { MapTo } from '@adobe/cq-angular-editable-components';
-
Create an editconfig obj which will be used for displaying some text when the component is empty, in this case given that the component’s properties like the ones defined on the dialog are available through the angular’s cqModel object we can just check if cqModel.text doesn’t have a value so that we can mark this component as empty
const BasicComponentEditConfig = { emptyLabel: 'Basic Component', isEmpty: cqModel => !cqModel || !cqModel.text || cqModel.text.trim().length < 1 };
-
Then we have the component which looks like a typical angular component. Here we declare that we are going to receive the text and checkbox properties.
export class BasicComponentComponent implements OnInit { @Input() text: string; @Input() checkbox: boolean; ...
-
Finally we call the MapTo helper function that maps the angular component to a particular resourceType inside AEM so that aem knows which angular component to use when rendering our page
MapTo('angularjs-simple-example/components/basic-component')( BasicComponentComponent, BasicComponentEditConfig );
-
Then inside the app.module.ts file we just add an entry into the entryComponents array given that the basic component is added dynamically.
@NgModule({ ... entryComponents: [TextComponent, PageComponent, BasicComponentComponent], ... }) export class AppModule {}
-
Build and deploy the application again
mvn clean install -PautoInstallPackage -Padobe-public
-
- 4.
Comentarios
Publicar un comentario