In this project we'll create a basic Angular application from scratch. Getting used to structuring your Angular application is an important skill to get right. Many times as beginners the hardest part about Angular is figuring out where to put stuff. Hopefully this project will start you on the right path so you can correctly structure all your Angular projects in the future.
- Fork and clone this repository.
- Open the project directory in your coding IDE.
In this step, we'll create the skeleton of our HTML. This includes an index.html
, styles.css
, and a js
folder.
- Create an
index.html
file.- Be sure to include a
DOCTYPE
tag at the top. - Be sure to include a
html
tag. - Be sure to include a
head
andtitle
tag. - Be sure to include a
body
tag.
- Be sure to include a
- Add a
paragraph
element to the html that says 'Woo'. - Create a
styles.css
file.- Add css that changes the font color of the
paragraph
element tored
.
- Add css that changes the font color of the
- Link
styles.css
in yourindex.html
file.- Once you have confirmed your
styles.css
is linked in yourindex.html
file, remove theparagraph
element fromindex.html
and the styles fromstyles.css
.
- Once you have confirmed your
index.html
<!DOCTYPE html>
<html>
<head>
<title>My first angular app!</title>
<link rel="stylesheet" href="./styles.css">
</head>
<body>
</body>
</html>
In this step, we'll turn our plain web application into an Angular application. We'll do this by acquiring angular's CDN, specifying our application's name, and specifying our main controller's name.
- Open a browser window and navigate to Angular JS
- Click on the download button and then click on
DOWNLOAD ANGULARJS
and copy the CDN link. - Open
index.html
and add ascript
tag that links to the Angular CDN just before the closingbody
tag. - Add a
ng-app
attribute to thehtml
tag. Let's call our Angular application"friendsList"
. - Add a
ng-controller
attribute to thebody
tag. Let's call our Angular controller"mainCtrl"
.
index.html
<!DOCTYPE html>
<html ng-app="friendsList">
<head>
<title>My first angular app!</title>
<link rel="stylesheet" href="./styles.css">
</head>
<body ng-controller="mainCtrl">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.32/angular.min.js"></script>
</body>
</html>
In this step, we'll create the javascript file that creates our Angular application.
- Create a file called
app.js
injs/
. - Open
js/app.js
. - Initialize an Angular application called
"friendsList"
. - Open
index.html
and add ascript
tag that links tojs/app.js
.- The order is imperative here. Include
js/app.js
just below the Angular CDN.
- The order is imperative here. Include
js/app.js
angular.module("friendsList", []);
index.html
<!DOCTYPE html>
<html ng-app="friendsList">
<head>
<title>My first angular app!</title>
<link rel="stylesheet" href="./styles.css">
</head>
<body ng-controller="mainCtrl">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.32/angular.min.js"></script>
<script src="./js/app.js"></script>
</body>
</html>
In this step, we'll create the javascript file that creates our main Angular controller.
- Create a filed called
mainCtrl.js
injs/
. - Open
js/mainCtrl.js
. - Create an Angular controller called
mainCtrl
. - Assign an array of your friends to a scope variable called
friends
. - Open
index.html
and add ascript
tag that links tojs/mainCtrl.js
.- The order is imperative here. Include
js/mainCtrl.js
just below thejs/app.js
.
- The order is imperative here. Include
js/mainCtrl.js
angular.module('friendsList').controller('mainCtrl', function($scope){
$scope.friends = [ 'Garry', 'Jerry', 'Larry', 'Perry', 'Mary' ];
});
index.html
<!DOCTYPE html>
<html ng-app="friendsList">
<head>
<title>My first angular app!</title>
<link rel="stylesheet" href="./styles.css">
</head>
<body ng-controller="mainCtrl">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.32/angular.min.js"></script>
<script src="./js/app.js"></script>
<script src="./js/mainCtrl.js"></script>
</body>
</html>
In this step, we'll take data from js/mainCtrl.js
's scope and output it in index.html
.
- Open
index.html
. - Create a
H1
tag that says"My Friends"
. - Under that
H1
tag useng-repeat
to create a list of friends.- The format should be a parent
div
element that has one childparagraph
element. - The
paragraph
element should display the name of the friend.
- The format should be a parent
index.html
<!DOCTYPE html>
<html ng-app="friendsList">
<head>
<title>My first angular app!</title>
<link rel="stylesheet" href="./styles.css">
</head>
<body ng-controller="mainCtrl">
<h1>My Friends</h1>
<div ng-repeat="friend in friends">
<p>{{ friend }}</p>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.32/angular.min.js"></script>
<script src="js/app.js"></script>
<script src="js/mainCtrl.js"></script>
</body>
</html>
- Add a function on
mainCtrl
's scope that can add friend names to thefriends
array on scope.- Call this function
addFriend
.
- Call this function
- Add an input field that a user can type a name into in
index.html
. - Add a button in
index.html
that a user can click to invokeaddFriend
off of scope.
You should then be able to see friends added onto the DOM automagically!
If you see a problem or a typo, please fork, make the necessary changes, and create a pull request so we can review your changes and merge them into the master repo and branch.
© DevMountain LLC, 2017. Unauthorized use and/or duplication of this material without express and written permission from DevMountain, LLC is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to DevMountain with appropriate and specific direction to the original content.