#Angular part 2
In the last workshop we covered the basics of building an Angular app. As a recap the steps included:
- Start with a default file to serve, typically
index.html
- Include the Angular Library source file like any other javascript file
- Create a main file,
app.js
, that defines and initiates the angular app 'Education' - Inside
app.js
should be a controller named'SchoolController'
, that defines a school property on the$scope
object, containing details about Fullstack Academy - On
index.html
, setng-app
to encompass the html body - In another div, declare the controller responsible for the scope of class schools
We were able to display a single object with multiple properties on the last workshop. The objective today is to display an array of multiple objects with similar properties.
To start, we need to fork the repository.
- Fork the repository to your account.
- Go to your own repositories on GitHub and clone the fellowshipt repository link.
- Use the link in the
git clone
then follow the rest of the commands.
$ git clone https://github.com/alwang85/fellowship.git
Leveraging the existing template, try displaying the contents of the following array:
schools = [{
students: 28,
name: "Fullstack Academy",
history: "2 years",
fellows: ["Seema", "Ivan", "Linda", "Sarah"],
motto: "code for life"
},{
students: 50,
name: "General Assembly",
history: "2 years",
fellows: ["Sam", "John", "George"],
motto: "all you can join"
},{
students: 17,
name: "Joescript Academy",
history: "Joe years",
fellows: ["Joe", "Joe", "Joe", "Joe"],
motto: "got Joe?"
}];
Bonus: When you are done, try out what happens when you delete a school from the schools array without modifying index.html
!
Chances are on index.html
you did something such as {{schools[0].name}}
for all 3 schools. That certainly goes against the coding mantra of DRY (Don't Repeat Yourself)!
Fortunately, Angular has a built in directive called ng-repeat
. Check out the following resources and then refactor your code using ng-repeat
!
https://docs.angularjs.org/api/ng/directive/ngRepeat
http://randomjavascript.blogspot.com/2014/09/building-angular-list-using-ng-repeat.html
Once you got ng-repeat
to work, try copying the same objects inside the schools array and add a duplicate copy into the array!
ng-repeat takes the form of <div ng-repeat="(key, value) in myObj"> ... </div>
for Objects, and <div ng-repeat="element in elements"> ... </div>
for an Array.
{{element.name}}
gives you access to the name property of this element in an Array. You may have noticed that every repetition has different values, even though they are referenced with the same "element.name"
. Every element in ng-repeat
has its own scope!
Also, Angular by default does not allow duplicate elements to be shown in ng-repeat, since it would be much harder to manipulate the DOM with multiple objects having the same data;
When we displayed the array of 3 schools by manually adding <div>
's, it was very tedious to manage all of the elements of the array. Worse, when the manually added elements had a different length of what's available in the array, the code broke.
In many applications, you may not be able to know beforehand the elements of what you wish to display. ng-repeat
is one of the BEST directives in Angular that helps front-end development become more DRY.
ng-repeat
keeps track of the different elements created, and also maintains a separate scope for each element. This allows the same function on the scope to be passed the different values of each event! Find out more about the benefits of separate scope in the last Bonus step!
-
Instead of displaying the fellows as an array, try nesting ng-repeat!
-
You can add a class to an element by adding
ng-class="classname"
to an element -
- Check the docs for
ng-class-odd
andng-class-even
to see how they work together withng-repeat
!
- Check the docs for
-
Check the documentation for
ng-click
, define a function in the controller, and experiment withng-click
! A good sample function would be:$scope.tellMe = function(element){ console.log(element) };