-
Notifications
You must be signed in to change notification settings - Fork 2
How to add a new component
Cathal Mac Donnacha edited this page Nov 2, 2015
·
1 revision
##How to add a new component
As you can see in this boilerpate project we have 6 sample components: about, common, home, layout, sample and topbar. Well, what would you do if you wanted to add your own component? For now, you have to copy and paste a template component called sample and manually rename the files and contents, but hopefully I'll automate this process in the future. Anyway, here's how you'd do it:
- Copy the
samplefolder and paste it into theappfolder with all the other components. - Rename the folder. Let's call it
contactUsas an example. - You can delete any sub-folders you don't think you'll use right away (i.e assests, unit, directives etc.). For this example, I'm going to keep all sub-folders.
- Expand the
controllersfolder. - Open the
controllers.jsfile. - Change all instances of
sampletocontactUs. Use title casing for controller names (e.g ContactUsCtrl). - In the
controllersfolder, rename theSampleCtrl.jsfile toContactUsCtrl.js. - Open the
ContactUsCtrl.jsfile. - Change all instances of
sampletocontactUs. - Expand the
directivesfolder - Open the
directives.jsfile. - Change all instances of
sampletocontactUs. - In the
directivesfolder, rename thesampleDirective.jsfile tocontactUsDirective.js. - Open the
contactUsDirective.jsfile. - Change all instances of
sampletocontactUs. - Expand the
e2efolder. - Rename the
sample.e2e.jsfile tocontactUs.e2e.js. - Open the
contactUs.e2e.jsfile. - In the first line, change
test sampletotest contact us. - Expand the
servicesfolder. - Open the
services.jsfile. - Change all instances of
sampletocontactUs. Use title casing for service names (e.g ContactUsService). - In the
servicesfolder, renameSampleService.jstoContactUsService.js. - Open the
ContactUsService.jsfile. - Change all instances of
sampletocontactUs. - Expand the
stylesfolder. - Open the
_styles.scssfile. - Change all instances of
sampletocontactUs. - In the
stylesfolder, rename the_sample.scssfile to_contactUs.scss. - Expand the
unitfolder. - Rename the
SampleCtrl.spec.jsfile toContactUsCtrl.spec.js. - Open the
ContactUsCtrl.spec.jsfile. - Change all instances of
sampletocontactUs. - In the
unitfolder, rename theSampleService.service.spec.jsfile toContactUsService.service.spec.js. - Open the
ContactUsService.service.spec.jsfile. - Change all instances of
sampletocontactUs. - In the
unitfolder, rename theSampleCtrl.spec.jsfile toContactUsCtrl.spec.js. - Open the
ContactUsCtrl.spec.jsfile. - Change all instances of
sampletocontactUs. - Expand the
viewsfolder - Rename the
sample.tpl.htmlfile tocontactUs.tpl.html. - Open the
contactUs.tpl.htmlfile. - Change
ng-controller="SampleCtrl"tong-controller="ContactUsCtrl" - Rename the
sample.module.jsfile tocontactUs.module.js - Open the
contactUs.modeule.jsfile. - Change all instances of
sampletocontactUs. - Open the
app.jsfile in the mainappfolder. - Add
'app.contactUs'to the list of component declarations. (Don't forget to add a comma to the end of the last entry, which would be'app.about'in this case. - Save all changes.
- Stop the
grunt watchtask if it's running. - Run the
grunt cleantask. - Run the
grunt watchtask. - Go to
http://localhost:9000 - Your brand new component is now ready to be used.
- To test if it worked go to
topbar > controllers > TopbarCtrl.js. - Change
$state.go('about');to$state.go('contactUs'); - On the topbar, click on the "Show About Page" button.
- You should see the text "This is a sample component".
Notes:
- Like I said, this is a pretty long winded approach so I'll hopefully automate this process in the future.
- If you are seeing an AngularJS dependency error in the browser's console then make sure that you've renamed everything from
sampletocontactUsand have includedapp.contactUsas a dependency in theapp.jsfile.