Sibeesh Passion

Top Menu

  • Home
  • Search
  • About
  • Privacy Policy

Main Menu

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Home
  • Search
  • About
  • Privacy Policy

logo

Sibeesh Passion

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment

  • Build, Deploy, Configure CI &CD Your Static Website in 5 mins

  • Post Messages to Microsoft Teams Using Python

  • Get Azure Blob Storage Blob Metadata Using PowerShell

  • Deploy .net 6 App to Azure from Azure DevOps using Pipelines

AngularHow to
Home›Angular›Basics of AngularJS

Basics of AngularJS

By SibeeshVenu
March 29, 2015
3554
8
Share:
Basics of Angular JS: Image 1

[toc]

Introduction

In this post we are going to discuss about AngularJS Most of you are familiar with the word AngularJS. This article is for those beginners that have not yet tried to implement AngularJS.

Download the source code

  • Basics of AngularJS
  • What Angular JS is?

    AngularJS has been introduced by the giant Google. It is a framework that helps you create dynamic web apps. Normally AngularJS uses HTML as the backbone. AngularJS creates extended HTML tags that can be used as normal HTML tags. These tags will help you to write efficient code. The interesting fact is you can reduce lines of codes you may need to write when you use normal JavaScript.

    Using the code

    So let’s start using AngularJS. What would be the first step you need to do? That would be to include the relevant JavaScript file as the following.

    [js]
    <script src=“~/Script/angular.min.js”></script>
    [/js]

    Once you include this file, you are free to enter the world of AngularJS. You can download that file from https://angularjs.org/

    Basic Directives

    There are some basic directives that you must be aware of in AngularJS. Those are,

    • ng-app
    • ng-init
    • ng-model
    • ng-repeat

    We will discuss them. Let’s start with a basic example.

    As I said before, AngularJS uses our HTML to work. So here we will create HTML with minimal tags. Are you ready?

    [html]
    <div ng-app>
    <p>My name is : {{“Please make me upper case letter”.toUpperCase()}}</p>
    </div>
    [/html]

    In the preceding example we added the tag ng-app, right? So what is ng-app?

    ng-app

    The ng-app tag indicates the root element of our angular application. It normally acts as the owner of the application. We can use AngularJS only after the declaration of this tag. The important point to remember is that we can’t use AngularJS before this tag. We will explain this with an example.

    What if we add a calculation of two numbers before the ng-app tag? We will check it out.

    [html]
    <p>Add me please: 2+2: {{ 2+2 }}</p>
    <div ng-app>
    <p>My name is : {{“Please make me upper case letter”.toUpperCase()}}</p>
    </div>
    [/html]

    What would be the output of this? Any guess?

    Basics of Angular JS Image 1

    Basics of Angular JS Image 1

    So what we need to make this work? For that you must put the code after the ng-app declaration. Can we try that? So our modified code would be as follows.

    [html]
    <div ng-app>
    <p>My name is : {{“Please make me upper case letter”.toUpperCase()}}</p>
    <p>Add me please: 2+2: {{ 2+2 }}</p>
    [/html]

    Now our output is as preceding.

    Basics of Angular JS Image 2

    Basics of Angular JS Image 2

    Well, that was the introduction to the directive ng-app. I hope you tried it. Now we will move on to the next one. What is it?

    ng-init

    As the name implies, ng-init is used to initialize the data of our AngularJS. We will try a demo. So the following is my HTML.

    [html]
    <div ng-init=“myFavoriteWebsites=[‘http://www.c-sharpcorner.com/’,’http://www.codeproject.com/’,’http://sibeeshpassion.com/’]”>
    My First fav website is : {{myFavoriteWebsites[0]}}
    <br />
    My Second fav website is : {{myFavoriteWebsites[1]}}
    <br />
    My Third fav website is : {{myFavoriteWebsites[2]}}
    </div>
    [/html]

    Please note that I have used the ng-init tag after the ng-app tag. Now can you guess what the output would be?

    Basics of Angular JS Image 3

    Basics of Angular JS Image 3

    I hope you understand what exactly ng-init tag is. Now we will see ng-model.

    ng-model

    Basically the ng-model directive binds the values from our HTML controls to our application data. Sounds good, right?

    [html]
    Currency in INR: <input type=“number” ng-model=“curinr” />
    <br />
    Currency in Dollar: {{curinr*62.27}}
    [/html]

    In the preceding example, we are using ng-model “curinr” and we are accessing that in our application data. In this process, whenever you type any value into the number box area, in a fraction of time you can see the calculated value. That is the power of AngularJS.

    Basics of Angular JS Image 4

    Basics of Angular JS Image 4

    So I hope you understood how to use the ng-model directive. Now it is time to move on to our next directive, ng-repeat.

    ng-repeat

    This directive is used for looping through a collection or array just like we use a foreach loop in C#. Sounds pretty cool, right? It clones the HTML elements once for each item in the collection.

    To work with ng-repeat we will see our previous example. We will modify that example as follows:

    [html]
    <div ng-init=“myFavoriteWebsites=[‘http://www.c-sharpcorner.com/’,’http://www.codeproject.com/’,’http://sibeeshpassion.com/’]”>
    <ul>
    My favorite websites are:
    <li ng-repeat=“n in myFavoriteWebsites”>
    {{n}}
    </li>
    </ul>
    </div>
    [/html]

    Now see the output below. With less lines of codes we have done this process. That is all about the ng-repeat directive. I hope you liked it.

    Basics of Angular JS Image 5

    Basics of Angular JS Image 5

    When you write any code, it must be light weight, efficient and easy right? If it is not easy, someone who looks at your code will be

    Basics of Angular JS Image 6

    Basics of Angular JS Image 6

    So we have controllers.

    What is controllers?

    Angular JS controllers are Java script object. Simple . The main functionality of this object is controlling the data of our angular application. Sounds cool right?

    The directive used here is ng-controller.

    In the previous section we used ng-model right? We have seen an example too. Now we will reuse that example here.

    The following is the example we done with ng-model

    [html]
    Currency in INR: <input type=“number” ng-model=“curinr” />
    <br />
    Currency in Dollar: {{curinr*62.27}}
    [/html]

    No we will change it to as follows.

    [html]
    <b>ng-Controller</b><br />
    <div >
    Currency in INR:
    <input type=“number” ng-model=“inrvalue” />
    <br />
    Currency in Dollar: {{inrToDollar()}}
    </div>
    [/html]

    In the above piece of code you can find a new directive ng-controller and I have assigned a valueInrToDollar . So our next step is to create that controller.

    We can create a controller as follows.

    [js]
    var my = angular.module(‘angularBasic’, []);
    [/js]

    Here ‘angularBasic’ is our application name. The complete code for creating our controller is as follows.

    [js]
    <script>
    var my = angular.module(‘angularBasic’, []);
    my.controller(‘InrToDollar’, function ($scope) {
    $scope.inrToDollar = function () {
    return $scope.inrvalue * 62.27;
    }
    });
    </script>
    [/js]

    What is $scope in the above code block?

    $scope is a part of controller. Every controller must have its own $scope object. Here what exactly our controller do is setting the behaviors on $scope.

    In the above code block we used a function which returns a dollar value for the given Indian rupee. I have given the function body inside of our controller.

    No what would be our output?

    Basics of Angular JS Image 7

    Basics of Angular JS Image 7

    Basics of Angular JS Image 8

    Basics of Angular JS Image 8

    Now think if you are using normal java script and jquery for doing this task, how many lines of codes you may need to write?

    Complete Code

    [html]
    @{
    ViewBag.Title = “Index”;
    }
    <h2>Index</h2>
    <b>ng-app</b>
    <br />
    <div ng-app=“angularBasic” ng-controller=“InrToDollar”>
    <p>My name is : {{“Please make me upper case letter”.toUpperCase()}}</p>
    <p>Add me please: 2+2: {{ 2+2 }}</p>
    <b>ng-init</b><br />
    <div ng-init=“myFavoriteWebsites=[‘http://www.c-sharpcorner.com/’,’http://www.codeproject.com/’,’http://sibeeshpassion.com/’]”>
    My First fav website is : {{myFavoriteWebsites[0]}}
    <br />
    My Second fav website is : {{myFavoriteWebsites[1]}}
    <br />
    My Third fav website is : {{myFavoriteWebsites[2]}}
    </div>
    <br />
    <b>ng-model</b><br />
    Currency in INR:
    <input type=“number” ng-model=“curinr” />
    <br />
    Currency in Dollar: {{curinr*62.27}}
    <br />
    <b>nd-repeat</b><br />
    <div ng-init=“myFavoriteWebsites=[‘http://www.c-sharpcorner.com/’,’http://www.codeproject.com/’,’http://sibeeshpassion.com/’]”>
    <ul>
    My favorite websites are:
    <li ng-repeat=“n in myFavoriteWebsites”>{{n}}
    </li>
    </ul>
    </div>
    <b>ng-Controller</b><br />
    <div >
    Currency in INR:
    <input type=“number” ng-model=“inrvalue” />
    <br />
    Currency in Dollar: {{inrToDollar()}}
    </div>
    <br />
    </div>
    <script src=“~/Script/angular.min.js”></script>
    <script>
    var my = angular.module(‘angularBasic’, []);
    my.controller(‘InrToDollar’, function ($scope) {
    $scope.inrToDollar = function () {
    return $scope.inrvalue * 62.27;
    }
    });
    </script>
    [/html]

    Conclusion

    Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.

    Your turn. What do you think?

    A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.

    Kindest Regards
    Sibeesh Venu

    TagsAngular directivesAngular JSAngular JS scopesAngular JS stepsAngular methodsBasics of Angular JS
    Previous Article

    Basic Difference Between Local Storage and Session ...

    Next Article

    Learning AngularJS: $http

    0
    Shares
    • 0
    • +
    • 0
    • 0
    • 0

    SibeeshVenu

    I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer.

    Related articles More from author

    • AngularHow to

      Implement Shared Custom Validator Directive in Angular

      April 22, 2018
      By SibeeshVenu
    • Angular_JS_Autocomplete_In_MVC_With_Web_API_Output_
      .NETAngularASP.NETWeb API

      Angular JS AutoComplete In MVC With Web API

      March 24, 2016
      By SibeeshVenu
    • Chart Widgets With Server Side Data In MVC Using Angular JS And Web API Output
      .NETAngularASP.NETWeb API

      Chart Widgets With Server Side Data In MVC Using Angular JS And Web API

      March 17, 2016
      By SibeeshVenu
    • Insertion_In_Table
      .NETAngularSQLWeb API

      TagIt Control With Data From Database Using Angular JS In MVC Web API

      February 25, 2016
      By SibeeshVenu
    • Custom Deffered Grid Using MVC Web API And Angular JS Deferred Output
      .NETAngularWeb API

      Custom Deferred Grid Using MVC Web API And Angular JS

      February 29, 2016
      By SibeeshVenu
    • jQuery Datatable With Server Side Data
      .NETAngularWeb API

      jQuery Datatable With Server Side Data

      February 25, 2016
      By SibeeshVenu
    0

    My book

    Asp Net Core and Azure with Raspberry Pi Sibeesh Venu

    YouTube

    MICROSOFT MVP (2016-2022)

    profile for Sibeesh Venu - Microsoft MVP

    Recent Posts

    • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment
    • Build, Deploy, Configure CI &CD Your Static Website in 5 mins
    • Easily move data from one COSMOS DB to another
    • .NET 8 New and Efficient Way to Check IP is in Given IP Range
    • Async Client IP safelist for Dot NET
    • Post Messages to Microsoft Teams Using Python
    • Get Azure Blob Storage Blob Metadata Using PowerShell
    • Deploy .net 6 App to Azure from Azure DevOps using Pipelines
    • Integrate Azure App Insights in 1 Minute to .Net6 Application
    • Azure DevOps Service Connection with Multiple Azure Resource Group

    Tags

    Achievements (35) Angular (14) Angular 5 (7) Angular JS (15) article (10) Article Of The Day (13) Asp.Net (14) Azure (65) Azure DevOps (10) Azure Function (10) Azure IoT (7) C# (17) c-sharp corner (13) Career Advice (11) chart (11) CSharp (7) CSS (7) CSS3 (6) HighChart (10) How To (9) HTML5 (10) HTML5 Chart (11) Interview (6) IoT (11) Javascript (10) JQuery (82) jquery functions (9) JQWidgets (15) JQX Grid (17) Json (7) Microsoft (8) MVC (20) MVP (9) MXChip (7) News (18) Office 365 (7) Products (10) SQL (20) SQL Server (15) Visual Studio (10) Visual Studio 2017 (7) VS2017 (7) Web API (12) Windows 10 (7) Wordpress (9)
    • .NET
    • Achievements
    • ADO.NET
    • Android
    • Angular
    • Arduino
    • Article Of The Day
    • ASP.NET
    • Asp.Net Core
    • Automobile
    • Awards
    • Azure
    • Azure CDN
    • azure devops
    • Blockchain
    • Blog
    • Browser
    • C-Sharp Corner
    • C#
    • Career Advice
    • Code Snippets
    • CodeProject
    • Cognitive Services
    • Cosmos DB
    • CSS
    • CSS3
    • Data Factory
    • Database
    • Docker
    • Drawings
    • Drill Down Chart
    • English
    • Excel Programming
    • Exporting
    • Facebook
    • Fun
    • Gadgets
    • GitHub
    • GoPro
    • High Map
    • HighChart
    • How to
    • HTML
    • HTML5
    • Ignite UI
    • IIS
    • Interview
    • IoT
    • JavaScript
    • JQuery
    • jQuery UI
    • JQWidgets
    • JQX Grid
    • Json
    • Knockout JS
    • Linux
    • Machine Learning
    • Malayalam
    • Malayalam Poems
    • MDX Query
    • Microsoft
    • Microsoft ADOMD
    • Microsoft MVP
    • Microsoft Office
    • Microsoft Technologies
    • Microsoft Windows
    • Microsoft Windows Server
    • Mobile
    • MongoDB
    • Monthly Winners
    • MVC
    • MVC Grid
    • MySQL
    • News
    • Node JS
    • npm
    • Number Conversions
    • October 2015
    • Office 365
    • Office Development
    • One Plus
    • Outlook
    • Page
    • PHP
    • Poems
    • PowerShell
    • Products
    • Q&A
    • Raspberry PI
    • React
    • SEO
    • SharePoint
    • Skype
    • Social Media
    • Software
    • Spire.Doc
    • Spire.PDF
    • Spire.XLS
    • SQL
    • SQL Server
    • SSAS
    • SSMS
    • Storage In HTML5
    • Stories
    • Third Party Software Apps
    • Tips
    • Tools
    • Translator Text
    • Uncategorized
    • Unit Testing
    • UWP
    • VB.Net
    • Videos
    • Virtual Machine
    • Visual Studio
    • Visual Studio 2017
    • Wamp Server
    • Web API
    • Web Platform Installer
    • Webinars
    • WebMatrix
    • Windows 10
    • Windows 7
    • Windows 8.1
    • Wordpress
    • Writing

    ABOUT ME

    I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer. If you would like to know more about me, you can read my story here.

    Contact Me

    • info@sibeeshpassion.com

    Pages

    • About
    • Search
    • Privacy Policy
    • About
    • Search
    • Privacy Policy
    © Copyright Sibeesh Passion 2014-2025. All Rights Reserved.
    Go to mobile version