A list of 60 AngularJS exercises, categorized into different topics to cover basics, data binding, directives, routing, services, and advanced features.
Basic AngularJS Exercises
- Set up a basic AngularJS application and display “Hello, AngularJS!”.
- Use
ng-app
to initialize an AngularJS app in an HTML document. - Bind a variable using
ng-bind
and display its value on the page. - Create an input field with two-way binding using
ng-model
. - Display a calculated result (e.g.,
{ { 2 + 2 } }
) using interpolation. - Create a button that changes a value in the controller using
ng-click
. - Create a list and use
ng-repeat
to loop through and display items. - Display a message only when a condition is true using
ng-if
. - Show or hide an element using
ng-show
andng-hide
. - Create a dropdown and display the selected value dynamically.
Controllers and Scope
- Define a controller and bind a variable to the
$scope
. - Use multiple controllers on the same page and pass data between them.
- Create a function in a controller and invoke it from a button click.
- Create a calculator app with addition, subtraction, multiplication, and division.
- Pass data from a parent controller to a child controller using
$scope
. - Use
$watch
to track changes in a variable. - Dynamically add items to a list using a controller function.
- Create a controller to handle form submission and validate input fields.
- Use
$scope
to bind a JSON object and display its properties. - Implement a toggle functionality for an element.
Directives
- Create a custom directive that displays a “Hello World” message.
- Use the
ng-init
directive to initialize variables. - Create a directive that accepts an attribute and displays it dynamically.
- Use the
ng-class
directive to conditionally apply CSS classes. - Use the
ng-style
directive to apply inline styles dynamically. - Build a custom directive to display user profile information.
- Create a directive to render a list of items.
- Create a reusable directive for a navigation bar.
- Use the
ng-repeat
directive with a custom filter. - Use a directive to format phone numbers dynamically.
Filters
- Use the
uppercase
filter to transform text. - Use the
currency
filter to format a number as currency. - Create a custom filter to reverse a string.
- Use the
orderBy
filter to sort an array dynamically. - Filter a list of items based on user input.
- Create a filter to display only even numbers from a list.
- Chain multiple filters to transform data.
- Use the
filter
filter to search within an array of objects. - Create a custom filter to mask sensitive information (e.g., credit card numbers).
- Format a date dynamically using the
date
filter.
Routing
- Set up basic routing with
ngRoute
. - Create multiple views and navigate between them using links.
- Use
$routeParams
to pass parameters to a route. - Add a default route using
otherwise
. - Create a 404 page for undefined routes.
- Use nested views with
ng-view
. - Implement route guards using a custom service.
- Load a view dynamically based on user authentication status.
- Preload data for a route using a
resolve
property. - Create a single-page application (SPA) with navigation and dynamic views.
Services and Dependency Injection
- Create a custom service to fetch data from a REST API.
- Use the
$http
service to perform a GET request. - Use the
$http
service to perform a POST request and handle the response. - Create a factory service to manage application state.
- Share data between controllers using a service.
- Implement a caching mechanism using
$cacheFactory
. - Use
$interval
to create a timer that updates every second. - Create a weather app using an external API and
$http
. - Handle errors in HTTP requests using
$http
’s.catch
method. - Build a shopping cart using a custom service to manage cart items.
Bonus Suggestions
For extended learning, you can:
- Create a form with validation for required fields, email formats, and more.
- Use
$location
to dynamically update the browser URL. - Implement authentication and session management.
- Create a dynamic dashboard with data fetched from APIs.
- Build a to-do app with persistent storage using localStorage or an external backend.