Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
AngularJS
2.1.
Example
2.1.1.
Code
2.1.2.
Output
3.
W3.CSS
3.1.
Examples
3.1.1.
Buttons
3.1.2.
Inputs
3.1.3.
Images
4.
Complete Implementation
4.1.
Code
4.2.
Output
5.
Frequently Asked Questions
5.1.
What is the difference between AngularJS and ReactJS?
5.2.
What is the benefit of learning modern W3.CSS?
5.3.
What classes does W3.CSS provide for buttons?
5.4.
What are some important features of AngularJS?
5.5.
What do you mean by templates in AngularJS?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

AngularJS and W3.CSS

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Hello Ninjas! You must have heard of AngularJS and W3.CSS. AngularJS is a famous Javascript framework, and W3.CSS is a modern CSS framework. We will be looking at both of them in detail and will also cover the example of how to use W3.CSS with AngularJS.

AngularJS and W3.CSS

Let's first start by learning about AngularJS.

AngularJS

AngularJS is a famous Javascript framework that is used for building web applications. It is preferred for creating Single Page Application projects (SPA). It helps extend HTML DOM with many additional attributes making it more responsive for user purposes. It is an open-source based framework used by thousands of developers all over the world.

AngularJS

Some of the features of AngularJS are:

  • AngularJS uses templates to define the structure of our application. These templates are written in HTML.
     
  • It uses a Model-View-Controller (MVC) architecture, which makes it easier to develop and maintain by separating the application into different components.
     
  • It provides routing support which helps us to build single-page applications with multiple views and navigate between them.
     
  • It provides built-in services like HTTP that helps you make requests to backend APIs and services.
     
  • It is a powerful framework for building interactive web applications rich in multiple features.

 

The AngularJS application is made up of various components such as directives, filters, providers, services, templates, etc. These components are contained in various modules such as ng, ngAnimate, ngAria, ngCookies, ngMock, etc.

You may refer to this article to learn in detail about modules.

 

To use AngularJS, we need to include it first in our HTML file, which can be done by using the script tag.

<script src="pathToYour/angular.min.js"></script>


Now let's see a basic example to show how AngularJS is used:

Example

We have shown the use of AngularJS using a simple web page. Let’s discuss the code:

  • First, we need to include the AngularJS library in our HTML code, which can be done using a script tag.
     
  • The ng-app directive is applied to the <body> element, which tells AngularJS that the entire <body> element and its children constitute an AngularJS application.
     
  • The ng-controller is applied to the <body> element and is named angularCtrl. It defines a scope variable named greeting with a value of "Hello Ninjas!".
     
  • The text of <p> tag "{{ greeting }}" is an AngularJS expression that is evaluated by the framework and replaced with the value of the greeting variable.
     

Code

<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>AngularJS Example</title>

	<!-- 
		Include AngularJS 
	-->
	<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.9/angular.min.js"></script>
</head>

<body ng-app="exampleApp" ng-controller="angularCtrl">
	<img src="http://www.codingninjas.com/blog/wp-content/uploads/2020/04/LOGO-05.png" alt="cn-logo" width="200px">
	
	<p style="margin: -5px 20px">{{greeting}}</p>

	<script>
		// AngularJS app and controller
		var app = angular.module("exampleApp", []);

		app.controller("angularCtrl", function($scope) {
			$scope.greeting = "Hello Ninjas!";
		});
	</script>
</body>

</html>


Output

Output
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

W3.CSS

W3.CSS is a modern CSS framework with built-in responsiveness. To use it on the web pages, just add a link to W3.CSS. It is comparatively easier than other CSS frameworks. It speeds up the tasks done and simplifies the web development process. It also supports modern devices like mobiles, tablets, laptops, and desktops. W3.CSS behaves to be a strong alternative to bootstrap, and it was developed by W3 School.

To use W3.CSS in our web pages, we need to include it using the following:

<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">

 

Some of the features of W3.CSS are:

  • It provides a responsive grid system that helps us to create a wide range of layouts.
     
  • It contains a large collection of CSS styles for elements like buttons, tables, forms, etc.
     
  • It can easily customize the appearance of web pages using CSS variables.
     
  • It helps us create layouts and styles for devices like mobiles, tablets, etc., by providing built-in support.
     
  • It makes it easier for web developers to make visually appealing websites.
     

Before moving on with the implementation, let’s see some of the examples.

Examples

Let’s discuss how W3.CSS styles can be applied to the following elements.

Buttons

We can create buttons with many different styles. We can create a solid button, add a border to a button, change the shape, or add extra padding around the button text.

Buttons

The above styles can be added using the following:

<!-- Solid colored button -->
<button class="w3-button w3-red">Red</button>
    
<!-- Add border to a button -->
<button class="w3-button w3-border w3-border-red">Border</button>
    
<!-- To chaneg button shape -->
<button class="w3-btn w3-black w3-round-xxlarge">Round</button>
    
<!-- Add extra padding around the button text -->
<button class="w3-btn w3-yellow w3-padding-large">Padding</button>


Inputs

We can customize the input tag in many ways. We can create bordered inputs or use colored labels to use different styles and colors.

Inputs

The above styles can be added using the following:

<!-- Normal input -->
<label>First Name</label>
<input class="w3-input inp" type="text">

<!-- Bordered input -->
<label>First Name</label>
<input class="w3-input w3-border inp" type="text">

<!-- colored labels -->
<label class="w3-text-blue"><b>First Name</b></label>
<input class="w3-input w3-border inp" type="text">


Images

We can add different styles to the images, like rounded corners, circle-shaped images, bordered images, etc.

Images

The above styles can be added using the following:

<!-- rounded corners -->
<img src="/ush.jpg" class="w3-round" alt="mountains">

<!-- circled image -->
<img src="/ush.jpg" class="w3-circle" alt="mountains">

<!-- bordered shape image -->
<img src="/ush.jpg" class="w3-border w3-padding" alt="mountains">

 

We are now familiar with both AngularJS and W3.CSS, so let's see the implementation of both.

Complete Implementation

Let’s create a simple calculator using AngularJS and W3.CSS. Follow these steps:

  • First, create an HTML file and include both AngularJS and W3.CSS in the <head> section.
     
  • Provide the <body> element with the ng-app and ng-controller attributes, which define the AngularJS module and controller that handles the logic of the application.
     
  • We need to create a form to take inputs and print the output:

    • Create a div and provide it with class w3-container and style it by providing a margin and background color. This can be done by adding classes w3-margin and w3-pale-yellow.
       
    • Inside the form tag, create two inputs and provide them labels. We can customize the input by providing classes like w3-input, w3-border, w3-round, etc
       
    • Bind the inputs to the variables num1 and num2 using the directive ng-model.
       
    • Create four buttons and set the ng-click directive to the function solve(), and pass the appropriate operation.
       
  • In the script tag, create an AngularJS module named exampleApp and declare a controller angularCtrl within the module, which is provided in the body element.
     
  • Define the function inside the controller, which will check the operator using a switch statement and, after performing the operation, update the $scope.ans variable. 
     
  • Access the answer using AngularJS expression binding, denoted by double curly braces {{}}, and display it using the paragraph tag.

Code

<!DOCTYPE html>
<html>

<head>
	<!-- 
		Include W3.CSS using this link  
	-->
	<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">

	<!-- 
		Including AngularJS using script tag 
	-->
	<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
</head>

<body ng-app="exampleApp" ng-controller="angularCtrl">

	<div class="w3-container cont w3-margin w3-pale-yellow" style="width:50%">
		<h2>Simple Calculator</h2>
		<form class="w3-margin-top">
			<label>Enter First Number: </label>
			<input type="number" class="w3-input w3-border w3-round w3-margin-bottom" style="width:60%" ng-model="num1"
				placeholder="Number 1" ng-required="true">

			<label>Enter Second Number: </label>
			<input type="number" class="w3-input w3-border w3-round w3-margin-bottom" style="width:60%" ng-model="num2"
				placeholder="Number 2" ng-required="true">

			<button class="w3-button w3-black w3-round btn" ng-click="solve('+')">
				+
			</button>
			<button class="w3-button w3-black w3-round btn" ng-click="solve('-')">
				-
			</button>
			<button class="w3-button w3-black w3-round btn" ng-click="solve('*')">
				x
			</button>
			<button class="w3-button w3-black w3-round btn" ng-click="solve('/')">
				/
			</button>
		</form>
		<p class="w3-text-red">The result is: {{ans}}</p>
	</div>

	<script>
		var app = angular.module('exampleApp', []);

		app.controller('angularCtrl', ['$scope', '$http', function ($scope, $http) {
			$scope.solve = (op) => {
				switch (op) {
					case '+':
						$scope.ans = $scope.num1 + $scope.num2;
						break;
					case '-':
						$scope.ans = $scope.num1 - $scope.num2;
						break;
					case '*':
						$scope.ans = $scope.num1 * $scope.num2;
						break;
					case '/':
						$scope.ans = $scope.num1 / $scope.num2;
						break;
				}
			}
		}]);
	</script>
</body>

</html>

Output

output

Frequently Asked Questions

What is the difference between AngularJS and ReactJS?

The main difference between both is that AngularJS is a full-featured framework, while ReactJS is a library for building user interfaces. This means that React is focused on rendering UI, while Angular is focused on building complete client-side applications.

What is the benefit of learning modern W3.CSS?

W3.CSS is a modern CSS framework that is designed to help developers build responsive and mobile-first websites more quickly and easily. It can be applied to your HTML elements to apply consistent styling, layout, and responsive behaviour.

What classes does W3.CSS provide for buttons?

The W3.CSS provides many classes for buttons. Some of them are w3-btn for creating a rectangular button, w3-block for defining a full-width button, w3-circle to create a circular button, w3-disabled to disable a button, etc.

What are some important features of AngularJS?

AngularJS allows you to sync data between your UI and your JavaScript code easily. It separates the application into different components (model, view, and controller) to make it easier to develop and maintain. It also provides support for routing.

What do you mean by templates in AngularJS?

In AngularJS, a template is an HTML file that contains a combination of HTML, AngularJS directives, and AngularJS expressions. AngularJS uses templates to define the structure of the UI for an application.

Conclusion

This article discussed AngularJS and W3.CSS. We have gone through some theoretical knowledge of the same. We have seen some important points about W3.CSS and also implemented it in AngularJS. To gain a complete understanding, practice it by yourself.

If you are interested to learn further, you can check out our other blogs:

 

You may refer to our Guided Path on Code Studios for enhancing your skill set on DSACompetitive ProgrammingSystem Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Happy Learning, Ninjas!

Live masterclass