Here's the first part of the series:
To setup AngularJS first go on AngularJS website & download AngularJS file or you can also use CDN link. Here I used CDN link in all my projects.
- Start Visual Studio
- Create new project on visual studio
- Create new folder on visual studio & add your AngularJS & css folder
- Create new html file name with hello program.
Add your CDN link on HTML file:
Run your html file by clicking on right side of html file & view in browser.
Output
- <!DOCTYPE html>
- <html ng-app>
-
- <head>
- <title>AngularJS</title>
- <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
- </script>
- </head>
-
- <body> Hello world.
- <p>The sum of 10 and 1 is equal to <b>{{10+1}}</b></p>
- </body>
- </html>
AngularJS Module
It is a container. It divided apps into small component whose reusable, functional component who’s integrated with other components.
- It has the place where we write AngularJS code for web app.
- Each module is identified by a unique name and can be dependent on other module.
- It is the place where we define dependencies.
- AngularJS module is single in every web page.
Why we use Module
- Modules are used to separate logics say services, controllers, application etc. and keep the code clean.
- We define modules in separate js files and name them as per the module.js file.
It has two types
- Application Module: It is used to initialize application with controller.
var mainApp = angular.Module (“mainApp”, []);
1st parameter - the name of the module.
2nd parameter - the array of module names on which this module is dependent on above code.
- Controller Module: It is used to define controller in web app.
As: MainApp. Controller (“student Controller”, function ($scope) {
}
Used component in module
- Directive: AngularJS directives are used to extend HTML.Starting with ng- prefix.
- Filter: Filters are used to change modify the data. It is using pipe character for filter data.
- Controller: A controller is defined using ng-controller directive. A controller is a JavaScript object containing attributes/properties and functions.
- Factory: A factory is a simple function which allows you to add some logic before creating the object. It returns the created object.
- Services: Services are JavaScript functions, which are responsible to perform only specific tasks.
- Provider: Provider is used by AngularJS internally to create services, factory, etc.
- Value: It is required to pass values to the controller during config phase (config phase is when AngularJS bootstraps itself).
- Config: It is used to inject module wise configuration settings to prevent accidental instantiation of services before they have been fully configured. This block is created using config () method.
- Route: Route, for switching views based on location (URL) changes.
Reference: AngularJS Tutorial
MVC Pattern
The core idea behind MVC is that you have clear separation in your code between managing its data (model), the application logic (controller), and presenting the data to the user (view).
- Open old project.
- Add new html File name MVC.
- Add code & run Program & watch Output.
Output
Complete Code
- <!DOCTYP Ehtml>
- <html>
-
- <head>
- <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js">
- </script>
- <meta charset="utf-8">
- <title>MVC pattern in AngulaJS</title>
- <script type="text/javascript"> var app = angular.module("app", []);
- </head>
- <body ng-app="app">
- <div ng-controller="Ctrl">
- <table>
- <tr>
- <td>Product Id :</td>
- <td>
- <spanng-bind="Product.id">
- </span>
- </td>
- </tr>
- <tr>
- <td>Name :</td>
- <td>
- <spanng-bind="Product.name">
- </span>
- </td>
- </tr>
- <tr>
- <td>salseman :</td>
- <td>
- <spanng-bind="Product.salseman">
- </span>
- </td>
- </tr>
- </table>
- </div>
- </body>
-
- </html>