ANGULARJS Development Guide 1:angularjs Introduction

Source: Internet
Author: User

What is AngularJS?

AngularJS is a structural framework designed for dynamic Web applications. It allows you to use HTML as a template language, extending the syntax of the HTML so you can build your application components more clearly and concisely. Its innovation is that with data binding and dependency injection, it allows you to no longer write large amounts of code.

Angularjs is designed to overcome the lack of HTML in building applications. HTML is a good declarative language for static text presentation design, but it seems weak to build a Web application.

In general, we use the following techniques to solve the disadvantages of static web page technology in building dynamic applications:

Class libraries-class libraries are a collection of functions that can help you write Web applications. It's your code that dominates, and it's up to you to decide when to use the class library. Class libraries are: jquery, etc.
Framework-Framework is a special, already implemented Web application that you only need to populate with specific business logic. The framework here is the dominant one, and it is used to invoke your code based on the specific application logic. The framework is: knockout, backbone and so on.

Angularjs uses a different approach, and it tries to make up for the drawbacks of HTML itself in building applications. Angularjs allows the browser to recognize the new syntax by using the structure we call Identifiers (directives). For example:

Use double curly braces {{}} syntax for data binding;
Use the DOM control structure to iterate or hide dom fragments;
Support validation of forms and forms;
The ability to associate logical code with related DOM elements;
The ability to compose HTML into reusable components.

ANGULARJS simplifies application development by presenting a higher level of abstraction for developers. As with other abstractions, this also loses some of the flexibility. In other words, not all applications are suitable for use with ANGULARJS. Angularjs is primarily concerned with building crud applications. Fortunately, at least 90% of Web applications are CRUD applications. But to understand what is appropriate to build with ANGULARJS, you need to understand what does not fit angularjs build. such as the game, graphical interface Editor, this DOM operation is very frequent and very complex applications, and crud applications are very different, they are not suitable for angularjs to build. Like this, it might be better to use some lighter, simpler technology jQuery .

A simple example of ANGULARJS

Index.html:

<!doctype html>

Script.js:

function Invoicecntl ($scope) {    = 1;     = 19.95;}

In the HTML tag, we use a ng-app identifier to indicate that this is a ANGULARJS application. This ng-app identifier will allow ANGULARJS to automatically initialize your app. We loaded the Angularjs script with the script tag: <script src= "Http://code.angularjs.org/angular-1.1.0.min.js" >
By setting the Ng-model property in the <input> tab, ANGULARJS automatically binds the data in both directions. We also performed some simple data validation:

When the page is loaded, Angularjs generates a variable of the same name according to the Model name (qty, cost) declared in the input tag. You can think of these variables as M (model) in the MVC design pattern;
Note that the INPUT element above has a special ability. If you do not enter data or the input data is invalid, this input box will automatically turn red. This new feature of the input box makes it easier for developers to implement the field validation features commonly found in crud applications.

Finally, we can look at this mysterious double brace {{}} : this {{expression}} tag is a ANGULARJS data binding. The expression can be a combination of the expression and the filter {{expression | filter}}. The ANGULARJS provides a filter to format input and output data.

In the above example, the expression in {{}} allows Angularjs to multiply the data obtained from the input box, then format the multiplication result into a local currency style, and then output it to the page.

It is worth mentioning that we do not have to call any Angularjs method, nor to write a specific logic like a framework to complete the above functions. This implementation is behind the fact that the browser does more work than the previous static page, allowing it to meet the needs of dynamic Web applications. ANGULARJS enables the development of dynamic Web applications to be reduced to a level that does not require a class library or framework.

Angularjs's philosophy

Angular believes that declarative code is much better than imperative code when building a view (UI) While writing software logic, although imperative code is well-suited to express business logic.

    • It is a very good idea to decouple DOM operation and application logic, which can greatly improve the regulation of code.
    • It is a very good idea to treat testing and development equally, and the difficulty of testing depends to a great extent on the structure of the Code;
    • Decoupling the client and server is a particularly good practice that enables both sides to develop concurrently, and allows both sides of the code to be reused;
    • If the framework can guide the developer throughout the development process: from designing the UI, to writing business logic, to testing, it will be a great help to developers;
    • "It is always good to simplify and simplify to zero".

Angularjs can free you from the following nightmares:

    • Using callbacks: The use of callbacks can disrupt the readability of your code, make your code fragmented, and be difficult to see the business logic. Removing some common code, such as callbacks, is a good thing. Drastically reducing the code you have to write because of the JavaScript language design will allow you to see the logic of your application more clearly.
    • Manually write code to manipulate DOM elements: manipulating the DOM is a fundamental part of AJAX applications, but it is always "cumbersome" and error-prone. The UI interface described declaratively can change as the application state changes, freeing you from writing low-level DOM manipulation code. In most applications written in Angularjs, developers do not have to write their own DOM code, but you can write them if you want.
    • Read and write data to the UI interface: A large part of AJAX applications is CRUD operations. A classic process is to set the server data into an internal object, then the object into an HTML form, the user modifies the form and then verify the form, if the error is displayed again, then the data is re-organized into internal objects, and then returned to the servers. There is too much code to repeat in this process, so that the code appears to always describe the entire execution process of the application, rather than the specific business logic and business details.
    • Before you start, you have to write a lot of basic code: usually you need to write a lot of basic code to implement a "Hello world" application. With ANGULARJS, it will provide services that make it easy for you to formally start writing your application, and these services are automatically added to your application via dependency injection, which allows you to quickly enter into the specific development of your application. In particular, you can master the initialization of automated tests.

Angular is one of the best in the MVVM model and is developed by Google, and there will be greater possibilities in the future.

Come on!

ANGULARJS Development Guide 1:angularjs Introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.