Basics of AngularJS

Basics of AngularJS: Part 1

By Gowtham Rajamanickam on Apr 09, 2015 AngularJS

I have planned to start writing about AngularJS. I have begun to learn AngularJS a few months back, it‘s a great feature for website UI developers. It is also easy to learn with interesting features available in this tutorial.

I have prepared a tutorial designed for the beginners wanting to learn the basics of AngularJS and its programming concepts in a simple and easy procedure. I will describe the components of AngularJS with suitable examples.

Angular is open source, completely free and used by thousands of developers around the world. It is licensed under the Apache license version 2.0.

AngularJS version 1.0 was released in 2012.

Miško Hevery, a Google employee, begun to work with AngularJS in 2009.

The idea turned out very well and the project is now officially supported by Google.

Reference for Angular js Tutorial:W3Schools,Tutorialspoint and C# Corner Member DJ for Angular Js.

What AngularJS is

Angular is a very powerful JavaScript library. It is used in Single Page Application (SPA) projects. It extends the HTML DOM with additional attributes and makes it more responsive to user actions. AngularJS is open source, completely free and used by thousands of developers around the world. It is licensed under the Apache License version 2.0.

Features

  • AngularJS is a powerful JavaScript based development framework to create RICH Internet Application (RIA).
  • Applications written in AngularJS is cross-browser compliant. AngularJS automatically handles JavaScript code suitable for each browser and it is a clean Model View Controller (MVC)

Prerequisites

  • Basic understanding of JavaScript and any text editor.
  • Web technologies such as HTML, CSS, AJAX and so on.
  • First, you will learn the basics of AngularJS: directives, expressions, filters, modules and controllers.
  • Then you will learn everything else you need to be familiar with Angular.
  • Events, DOM, Forms, Input, Validation, HTTP.
  • Any other editor like JsFiddle or W3 Schools editor to test the output.

The AngularJS framework can be divided into the following three major parts:

  • ng-app: This directive defines and links an AngularJS application to HTML.
  • ng-model: This directive binds the values of AngularJS application data to HTML input controls (input, select, text area).
  • ng-bind: This directive binds the AngularJS application data to HTML tags and views.

AngularJS is a JavaScript Framework

AngularJS is a JavaScript framework. It is a library written in JavaScript. Within the script tag use an Angular js reference file.

  1. <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>

Example

Step 1

The ng-app directive tells AngularJS that the <div> element is the "owner" of an AngularJS application.

Step 2

The ng-model directive binds the value of the input field to the application variable name.

Step 3

The ng-bind directive binds the innerHTML of the <p> element to the application variable name.

ng-init: The ng-init directive initializes AngularJS application variables.



Expressions: 
AngularJS expressions are written inside double braces: {{ expression }}. Sample: if you want to add a two digit use , {{4+5}}

AngularJS expressions bind AngularJS data to HTML the same way as the ng-bind directive.

You will learn about models and controllers later in this tutorial.

Basics of AngularJS: Part 2

By Gowtham Rajamanickam on Apr 14, 2015

I had planned to start writing about AngularJS. I began to learn AngularJS a few months back, it‘s a great feature for website UI developers. It is also easy to learn with the interesting features available in this tutorial.

I prepared a tutorial designed for the beginners wanting to learn the basics of AngularJS and its programming concepts in a simple and easy procedure. I will describe the components of AngularJS with suitable examples.

The reference for Angular js is Tutorial:W3Schools,Tutorialspoint and C# Corner Member DJ for Angular Js.

In my previous article I explained angujar js, its prerequisites, features and basic syntax.

Basics of AngularJS: Part 1

In this article I would like to share MVC patterns in Angular js.

  • Model

    It is the lowest level of the pattern responsible for maintaining data. The model is responsible for managing application data. It responds to the request from the view and to the instructions from the controller to update itself.

  • View

    The View is responsible for displaying all, or a portion of, the data to the user. A presentation of data in a specific format, triggered by the controller‘s decision to present the data. They are script-based template systems such as JSP, ASP, PHP and very easy to integrate with AJAX technology.

  • Controller

    A Controller is software code that controls the interactions between the Model and View. The controller responds to user input and performs interactions on the data model objects. The controller receives input, validates it and then performs business operations that modify the state of the data model.

    AngularJS is a MVC based framework. In the future chapters, we will see how AngularJS uses MVC methodology.

AngularJS Controllers

An AngularJS application mainly relies on controllers to control the flow of data in the application. A controller is defined using the ng-controller directive. A controller is a JavaScript object containing attributes/properties and functions.

Each controller accepts $scope as a parameter that refers to the application/module that the controller is to control.

  1. <div ng-app="" ng-controller="inputController">
  2. ...
  3. </div>

AngularJS applications are controlled by controllers.

The ng-controller directive defines the application controller.

A controller is a JavaScript Object, created by a standard JavaScript object constructor.

  1. < div ng - app = "myApp"
  2. ng - controller = "inputctrl" > First Name: < input type = "text"
  3. ng - model = "firstName" > < br > Last Name: < input type = "text"
  4. ng - model = "lastName" > < br > < br > Full Name:
  5. {
  6. {
  7. firstName + " " + lastName
  8. }
  9. }
  10. < /div>
  11. <script>
  12. var app = angular.module(‘myApp‘, []);
  13. app.controller(‘inputctrl‘, function($scope)
  14. {
  15. $scope.firstName = "";
  16. $scope.lastName = "";
  17. });
  18. </script >

Explanation

In this example ng-app runs inside a <div>.

This ng-controller directive defines a controller.

The inputtrl function is a JavaScript function. AngularJS will invoke the controller with a $scope object.

In AngularJS, $scope is the application object (the owner of the application variables and functions).

The controller creates two properties (variables) in the scope (firstName and lastName).

The ng-model directives bind the input fields to the controller properties (firstName and lastName).

In the preceding example we have used two properties, first name and last name.

The same as in the preceding example, here we are returning the first name and last name to the HTML value.

Explanation

  1. Full Name: {{fullName()}}
  2. $scope.fullName = function() 
    {
  3. return $scope.firstName + " " + $scope.lastName;
  4. }

Source Code

  1. <div ng-app="myApp" ng-controller="inputCtrl">
  2. First Name: <input type="text" ng-model="firstName"><br>
  3. Last Name: <input type="text" ng-model="lastName"><br>
  4. <br>
  5. Full Name: {{fullName()}}
  6. </div>
  7. <script>
  8. var app = angular.module(‘myApp‘, []);
  9. app.controller(‘ inputCtrl‘, function($scope) {
  10. $scope.firstName = "";
  11. $scope.lastName = "";
  12. $scope.fullName = function() {
  13. return $scope.firstName + " " + $scope.lastName;
  14. }
  15. });
  16. </script>

Controllers In External Files

We can use an external file also for this method. In larger applications, it is common to store controllers in external files.

The following is another example to bind the employee details from an external file:

  1. angular.module(‘myApp‘, []).controller(‘namesCtrl‘, function($scope) {
  2. $scope.Employees = [{
  3. name: ‘Gowtham‘,
  4. country: ‘karur‘
  5. }, {
  6. name: ‘kiruthiga‘,
  7. country: ‘Tirupur‘
  8. }, {
  9. name: ‘GK‘,
  10. country: ‘Chennai‘
  11. }];
  12. });

src: http://www.c-sharpcorner.com/UploadFile/91b369/basics-of-angular-js-part-1/

src: http://www.c-sharpcorner.com/UploadFile/91b369/basics-of-angularjs-part-2/

时间: 2024-10-12 12:47:24

Basics of AngularJS的相关文章

【转】Build Your own Simplified AngularJS in 200 Lines of JavaScript

原文:http://blog.mgechev.com/2015/03/09/build-learn-your-own-light-lightweight-angularjs/ Build Your own Simplified AngularJS in 200 Lines of JavaScript Edit My practice proved that there are two good/easy ways to learn a new technology: Re-implement i

Java软件工程师技能图谱

原文链接:Java软件工程师技能图谱 最近在考虑"拥有怎样的技能才能算一名合格的java软件工程师呢?"这个问题.碰巧在github发现一个很棒的开源项目--程序员技能图谱.@Zhang Wei写的Java Software Engineer Skill Map确实能解答我心中的疑问.好的东西应该向更多的人,我将英文版本根据自己的理解写成中文版,并补充了相应的学习资料(书籍是可下载的,中文书籍可能存在版权问题,推荐书籍都是比较经典的英文教材).希望这次整理能帮助更多的人解答心中的疑惑.

03基础-AngularJS基础教程

0. 目录 目录 前言 正文 1 Set up 2 表达式 3 指令 ng-bind ng-init ng-non-bindable ng-show 4 作用域 双向绑定Two-way binding ng-model 5 总结 声明 1. 前言 AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门很好的为静态文本展示设计的声明式语言,但要构建WEB应用的话它就显得乏力了,所以AngularJS做了一些工作来来解决静态网页技术在构建动态应用上的不足. AngularJS

04控制器-AngularJS基础教程

0. 目录 目录 前言 正文 1 ng-controller 2 构建模型-Constructing the model 3 控制器作为属性名-Controller as propertyName 4 依赖注入-Dependency injection 41 scope 5 模型视图控制器MVC-Model-view-controller 6 函数-Functions 7 回调函数 8 结论 声明 1. 前言 AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门很好的为

07模块化-AngularJS基础教程

0. 目录 目录 前言 正文 1 Why use Angular modules为什么使用模块化 2 创建应用模块-Creating the application module 3 加载应用模块-Loading the application module 4 定义控制器-Defining a controller 5 链式定义-Chaining definitions 6 加载模块-Loading modules 10 结论 声明 1. 前言 AngularJS是为了克服HTML在构建应用上

05作用域-AngularJS基础教程

0. 目录 目录 前言 正文 1 setup 2 scope 3 rootScope 4 Isolation隔离 5 Nesting嵌套 6 Inheritance继承 7 scopewatch 8 scopeapply 9 结论 声明 1. 前言 AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门很好的为静态文本展示设计的声明式语言,但要构建WEB应用的话它就显得乏力了,所以AngularJS做了一些工作来来解决静态网页技术在构建动态应用上的不足. AngularJ

06集合-AngularJS基础教程

0. 目录 目录 前言 正文 1 Set up 2 Iteration 3 ng-repeat 4 Object properties 5 index 6 ng-init 7 Uniqueness 71 严格相等 72 track by 8 Callback functions 9 -start and -end 10 结论 声明 1. 前言 AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门很好的为静态文本展示设计的声明式语言,但要构建WEB应用的话它就显得乏力了,

Ultimate guide to learning AngularJS in one day

What is AngularJS? Angular is a client-side MVC/MVVM framework built in JavaScript, essential for modern single page web applications (and even websites). This post is a full end to end crash course from my experiences, advice and best practices I've

AngularJs学习笔记--Modules

原版地址:http://code.angularjs.org/1.0.2/docs/guide/module 一.什么是Module? 很多应用都有一个用于初始化.加载(wires是这个意思吗?)和启动应用的main方法.angular应用不需要main方法,作为替代,module提供有指定目的的声明式,描述应用如何启动.这样做有几项优点: 这过程是声明描述的,更加容易读懂. 在单元测试中,不需要加载所有module,这对写单元测试很有帮助. 额外的module可以被加载到情景测试中,可以覆盖一