Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Some "rules"
Tell us about yourself.
Image Credit: Wikimedia
A basic todo list app
The app's markup
Download and serve local file
<script src="js/angular.min.js"><script>
OR
Use a content distribution network for a cached file
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
The Angular library looks for these attribute in your markup to know it should render Angular code
<body ng-app>
<h1>Hello World!</h1>
</body>
ng-app is a required directive used to identify the root element and determine scope
A way to insert data into html via a JavaScript string, number, object, or array
<body ng-app>
<p>I have {{ 1 + 2 }} apples</p>
<p>My cat's name is {{ "Admiral " + "Snuggles" }}</p>
</body>
The ng-model directive binds the value of an input
, select
, or textarea
to an object that can be used in an expression
<body ng-app>
<label>Name:</label>
<input type="text" ng-model="yourName">
<hr>
<h1>Hello {{yourName}}!</h1>
</body>
Expressions can be inserted as the value html attributes as well as text within html tags
<body ng-app>
<label>Name:</label>
<input type="text" ng-model="user.status">
<hr>
<h1 class="status-text-{{user.status}}">Your status is: {{user.status}}!</h1>
</body>
ng-repeat is Angular's way of looping
<body ng-app>
<ul>
<li ng-repeat="item in ['Sal', 'Jo', 'Amir', 'Maria']">
{{item}} is my friend.
</li>
</ul>
</body>
ng-if is Angular's if
statement
<body ng-app>
<p ng-if="'Sal' === 'Sal'">True!</p>
<p ng-if="'Sal' === 'Jo'">False!</p>
</body>
ng-show
& ng-hide
toggle an element's display based on value
<body ng-app>
<p ng-show="5> 2">Five is greater than two.</p>
<p ng-show="2> 5">Two is greater than five.</p>
</body>
Note: if the value of ng-show
evaluates to false, the element will still live in the DOM
ng-init
sets an initial value
<head>
<!-- add icon font library -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
</head>
<body ng-app>
<nav ng-init="isOpen=false" class="mobile-nav">
<button><i class="fa fa-bars"></i></button>
<ul ng-show="isOpen">
<li>Home</li>
<li>About</li>
<li>Blog</li>
</ul>
</nav>
</body>
Angular's click listener
<body ng-app>
<nav ng-init="isOpen = false" class="mobile-nav">
<button ng-click="isOpen = !isOpen">
<i class="fa fa-bars"></i>
</button>
<ul ng-show="isOpen">
<li>Home</li>
<li>About</li>
<li>Blog</li>
</ul>
</nav>
</body>
Bonus! Angular will make your ng-click
elements work with the keyboard too.
ng-class
conditionally adds a class based on value
<body ng-app>
<nav ng-init="isOpen = false" class="mobile-nav">
<p ng-class="{'highlight': isOpen}">My Menu</p>
<button ng-click="isOpen = !isOpen">
<i class="fa fa-bars"></i>
</button>
<ul ng-show="isOpen">
<li>Home</li>
<li>About</li>
<li>Blog</li>
</ul>
</nav>
</body>
Here's another example:
<body ng-app>
<nav ng-init="isOpen = false" class="mobile-nav">
<button ng-click="isOpen = !isOpen">
<i class="fa" ng-class="{
'fa-caret-square-o-down': !isOpen,
'fa-caret-square-o-up': isOpen}">
</i>
</button>
<ul ng-show="isOpen">
<li>Home</li>
<li>About</li>
<li>Blog</li>
</ul>
</nav>
</body>
section
or div
where you set a value to true or false with ng-init
ng-click
directive that toggles the ng-init
valueng-show
or ng-hide
to show/hide an image of your pet (or your favorite animal) on click of your button It's good to know how ng-init works, but usually you'll use a controller for that purpose
Before we talk about controllers, let's talk about modules!
ng-app
directives(function(){
angular.module('myApp', []);
})();
<body ng-app="myApp">
<p>I have {{ 1 + 2 }} apples</p>
<p>Our President is {{ "Barack " + "Obama" }}</p>
</body>
(function(){
angular
.module('myApp', [])
.controller('MainController', function(){
this.cat = cat;
});
var cat = {
name: 'Ms. Whiskers',
age: 9,
eyes: 'blue'
};
})();
<body ng-app="myApp">
<section ng-controller="MainController as mainCtrl">
<p>My cat's name is {{mainCtrl.cat.name}}</p>
<p>She has {{mainCtrl.cat.eyes}} eyes and she's
{{mainCtrl.cat.age}} years old.</p>
</section>
</body>
The scope of the controller is only within the section
element!
ng-app
directive with the value of "recipeApp", and an ng-controller
directive with the value of "RecipeController as recipeCtrl"ng-repeat
to iterate over one of your array properties in the recipe objectng-if
or ng-show
to conditionally display your boolean propertiesOften your data will be stored in an array, rather than a single object like we've seen so far
(function(){
angular
.module('myApp', [])
.controller('MainController', function(){
this.cats = cats;
});
var cats = [
{
name: 'Ms. Whiskers',
age: 9,
eyes: 'blue'
},
{
name: 'Melvin',
age: 1,
eyes: 'green'
},
{
name: 'Tootsie',
age: 3,
eyes: 'grey'
}
];
})();
If your data is stored in an array, you'll need to loop through it or access specific items by index number
<body ng-app="myApp">
<section ng-controller="MainController as mainCtrl">
<article ng-repeat="cat in mainCtrl.cats">
<p>My cat's name is {{cat.name}}</p>
<p>She has {{cat.eyes}} eyes and she's
{{cat.age}} years old.</p>
</article>
</section>
</body>
ng-repeat
to loop through your recipes array and display each one on the pageFunctions can also be stored as properties
//app.js
angular
.module('todoApp', [])
.controller('TodoListController', function() {
var todoList = this;
todoList.todos = [
{text:'learn angular', done:true},
{text:'build an angular app', done:false}
];
todoList.logArg = function(arg) {
console.log(arg);
};
});
And they can be called by events!
<!-- index.html -->
<body ng-app="todoApp">
<section ng-controller="TodoListController as todoListCtrl">
<button ng-click="todoListCtrl.logArg('hi!')">
Log me!
</button>
</section>
</body>
angular.module('todoApp', ['LocalStorageModule'])
.controller('TodoListController', function(localStorageService) {
var todoList = this;
// todoList.todos = [
// {text:'learn angular', done:true},
// {text:'build an angular app', done:false}];
var todosInStore = localStorageService.get('myTodos');
todoList.todos = todosInStore || [];
todoList.addTodo = function() {
todoList.todos.push({text:todoList.todoText, done:false});
localStorageService.set('myTodos', todoList.todos);
todoList.todoText = '';
};
[...]
});
'LocalStorageModule'
as a dependency and localStorageService
as an argument in your controller callback function todosInStore
with the value of localStorageService.get('myTodos')
todoList.todos
, and set todoList.todos
to: todosInStore || []
addTodo
function, before the line that sets an empty string, add a function to set local storage with the array of todos as its updated (localStorageService.set('myTodos', todoList.todos);
)$scope
in favor of using this
in our controllers to reference the application model$scope
object is being phased out of Angular, and using this
with 'controller as' syntax is now considered best practice$scope
object if we want to use some of the cool Angular features that come with it$watch
is an API that watches/listens for changes in user input, and can run callback functions when changes occur$scope.$watch
to save our checked off todo list items to local storageangular.module('todoApp', ['LocalStorageModule'])
.controller('TodoListController', function($scope, localStorageService) {
var todoList = this;
var todosInStore = localStorageService.get('myTodos');
todoList.todos = todosInStore || [];
$scope.$watch('todoList.todos', function () {
localStorageService.set('myTodos', todoList.todos);
}, true);
todoList.addTodo = function() {
todoList.todos.push({text:todoList.todoText, done:false});
//localStorageService.set('myTodos', todoList.todos);
todoList.todoText = '';
};
[...]
});
There are many more things you can do with Angular!
$scope
object & jqLite will be removed