Table of Contents
This article introduces the basic use of angularjs and the basic function introduction is here, so that you can have a clearer understanding of the functions of angularjs And its use, now let’s take a look at this article
" >This article introduces the basic use of angularjs and the basic function introduction is here, so that you can have a clearer understanding of the functions of angularjs And its use, now let’s take a look at this article
Introduction to the basic functions of AngularJS" >Introduction to the basic functions of AngularJS
Home Web Front-end JS Tutorial How much do you know about the basic functions of angularjs? Detailed introduction to the use of angularjs functions

How much do you know about the basic functions of angularjs? Detailed introduction to the use of angularjs functions

Sep 08, 2018 pm 02:14 PM
angular frame

This article introduces the basic use of angularjs and the basic function introduction is here, so that you can have a clearer understanding of the functions of angularjs And its use, now let’s take a look at this article

Introduction to the basic functions of AngularJS

AngularJS is a web application development framework launched by Google. It provides a series of well-compatible and extensible services, including data binding, DOM operations, MVC design pattern and module loading, etc. This article focuses on the use of AngularJS directives. Before we get into the topic, we will take a quick look at the basic usage of AngularJS.

AngularJS is not just a class library, but provides a complete framework. It avoids the tedious work of interacting with multiple class libraries and needing to be familiar with multiple sockets. Designed by the developers of Google Chrome, it leads the next generation of web application development. Maybe we won’t be using AngularJS in 5 or 10 years, but the essence of its design will always be used.

Developers who know AngularJS, you will definitely be excited about the AngularJS custom directive (its function is equivalent to the custom control under the .NET platform) feature. Custom directives allow you to extend HTML tags and attributes. Instructions can be reused and used across projects.

Custom instructions have been widely used, one of which is worth mentioning-Wijmo control set. It contains nearly 50 AngularJS-based controls. Wijmo is a set of HTML5 front-end controls for creating desktop and mobile web applications. From interactive charts to powerful table controls, Wijmo has almost everything we need. You can learn more about Wijmo from the official website. Therefore, Wijmo is a good reference example for learning AngularJS: AngularJS Directive Gallery

How much do you know about the basic functions of angularjs? Detailed introduction to the use of angularjs functions

# Creating custom directives is very easy. Instructions can be tested, maintained, and reused across multiple projects.

To use AngularJS, you need to reference the script file in the HTML page and add the ng-app attribute to the HTML or Body tag. Here is a simple example using AngularJS:

  
    <script></script>
  
  
    <input>
    <p>{{msg}}</p>
  
Copy after login

When AngularJS loads, it searches the document for the ng-app attribute. This tag is usually set to the main module of the project. Once discovered, Angular operates on the document.

In this example, the ng-init feature initializes a msg variable "Grape City Control Team Blog", and the ng-model feature bidirectionally binds it to the input control (note: the curly brackets are binding mark). AngularJS will parse this tag and update the msg text value in real time as the input value changes. You can view the effect from the link: Click to enter

How much do you know about the basic functions of angularjs? Detailed introduction to the use of angularjs functions

AngularJS module

The module can be said to be the foundation of AngularJS. It contains configuration, control, filtering, factory mode, instructions and other modules.

If you are familiar with the .NET platform but are initially learning Angular. The following table is a brief comparison to help you understand role playing in Angular:

AngularJS

.NET

Summary

module

Assembly

Application Development Module

controller

ViewModel

Controller, activate the organizational functions between different levels

scope

DataContext

Provide bound data for the view

filter

ValueConverter

Modify the data before transferring it to the view

directive

Component

Reusable UI elements can also be understood as front-end plug-ins

factory, service

Utility classes

Provide services for other module elements

例如,下面的代码使用控制器、过滤器和指令创建了一个模块:

// the main (app) modulevar myApp = angular.module("myApp", []);// add a controllermyApp.controller("myCtrl", function($scope) {
    $scope.msg = "grapecity team blog";
});// add a filtermyApp.filter("myUpperFilter", function() {    return function(input) {        return input.toUpperCase();
    }
});// add a directivemyApp.directive("myDctv", function() {    return function(scope, element, attrs) {
        element.bind("mouseenter", function() {
            element.css("background", "yellow");
        });            
        element.bind("mouseleave", function() {
            element.css("background", "none");
        });            
    }
});
Copy after login

上面示例中module 方法的第一个参数为模块的名称,第二个参数为它的依赖模块列表。我们创建了一个独立的模块,不依赖于其它模块。所以第二个参数为空数组(注意:即使它为空,我们也必须填写这个参数。否则,该方法回去检索之前的同名模块)。这部分我们将在后续的文章中详细阐述。(想看更多就到PHP中文网AngularJS开发手册中学习)

controller 构造函数获取$scope 对象,用于存储所有controller 暴露的接口和方法。scope 由Angular 传递到视图和指令层。在这个例子中, controller 添加了msg 属性给scope对象。一个应用模块可以包含多个controller,每个controller各司其职,控制一个或多个视图。

filter 构造函数返回一个方法用于更改input文本的显示方式。Angular 提供很多内置的filter,同时,你也可以添加自定义filter,操作方式Angular内置filter相同。在这个例子中,实现了小写到大写的转换。Filter不仅可以格式化文本值,还可以更改数组。AngularJS 内置的格式化Filter有number、date、currency、uppercase和lowercase。数组 filter有filter、orderBy和 limitTo。Filter需要设置参数,语法格式也是固定的:someValue |filterName:filterParameter1:filterParameter2....

directive 构造函数返回了一个方法,该方法用于传递一个元素,并依据scope中的参数对其进行修改。示例中我们绑定了mouseenter 和mouseleave 事件用于切换文本高亮显示。这是一个功能简单的指令,在后续的章节将展示如何创建一些复杂指令。

下面是使用模块构建的页面:

    <input>
    <p>
        {{msg | myUpperFilter }}    </p>
Copy after login

How much do you know about the basic functions of angularjs? Detailed introduction to the use of angularjs functions

 注意应用中module、controller和filter 作为特性值应用。它们代表JavaScript 对象,因此名称是区分大小写的。指令的名称同样也是属性值,它作为HTML标签被解析,所以也是区分大小写的。但AngularJS 会自动转换这些特性为小写,例如“myDctv" 指令变成"my-dctv" (就像内置的指令ngApp, ngController, 和ngModel会转换成 "ng-app", "ng-controller", 和"ng-model"。

 项目组织结构

使用AngularJS 可以创建大型Web项目。你可以把项目拆分为多个模块,把一个模块拆分为多个模块文件。同时,可以按照你的使用习惯组织这些文件。

列举一个典型的项目结构:

Root
        default.html
        styles
               app.css
        partials
               home.html
               product.html
               store.html
        scripts
               app.js
               controllers
                       productCtrl.js
                       storeCtrl.js
               directives
                       gridDctv.js
                       chartDctv.js
               filters
                       formatFilter.js
               services
                       dataSvc.js
               vendor
                       angular.js
                       angular.min.js

假设如果你仅希望项目中使用一个模块,你可以如此定义:

// app.jsangular.module("appModule", []);
Copy after login

 如果希望在模块中添加元素,你可以通过名称调用模块向其中添加。例如: formatFilter.js 文件包含以下元素:

// formatFilter.js// 通过名称获取模块var app = angular.module("appModule");// 向模块中添加filterapp.filter("formatFilter", function() {  return function(input, format) {    return Globalize.format(input, format);
  }
}})如果你的应用包含多个模块,注意在添加模块时添加其它模块的引用。例如,一个应用包含三个模块app、controls、和data :
Copy after login
// app.js (名称为app的模块依赖于controls和data模块)angular.module("app", [ "controls", "data"])// controls.js (controls 模块依赖于data 模块)angular.module("controls", [ "data" ])// data.js (data 模块没有依赖项,数组为空)angular.module("data", [])应用的主页面中需要声明ng-app 指令, AngularJS 会自动添加需要的引用:
Copy after login

...
Copy after login

进行以上声明后,你就可以在所有的页面中使用其它三个模块声明的元素了。

这篇文章中我们了解了AngularJS的基本使用方法及结构。在下一个章节中,我们将阐述基本的指令概念,同时,会创建一些实例来帮助你加深指令作用的理解。

好了,本篇文章到这就结束了(想看更多就到PHP中文网AngularJS使用手册中学习),有问题的可以在下方留言提问。

The above is the detailed content of How much do you know about the basic functions of angularjs? Detailed introduction to the use of angularjs functions. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1252
29
C# Tutorial
1226
24
How to evaluate the cost-effectiveness of commercial support for Java frameworks How to evaluate the cost-effectiveness of commercial support for Java frameworks Jun 05, 2024 pm 05:25 PM

Evaluating the cost/performance of commercial support for a Java framework involves the following steps: Determine the required level of assurance and service level agreement (SLA) guarantees. The experience and expertise of the research support team. Consider additional services such as upgrades, troubleshooting, and performance optimization. Weigh business support costs against risk mitigation and increased efficiency.

How does the learning curve of PHP frameworks compare to other language frameworks? How does the learning curve of PHP frameworks compare to other language frameworks? Jun 06, 2024 pm 12:41 PM

The learning curve of a PHP framework depends on language proficiency, framework complexity, documentation quality, and community support. The learning curve of PHP frameworks is higher when compared to Python frameworks and lower when compared to Ruby frameworks. Compared to Java frameworks, PHP frameworks have a moderate learning curve but a shorter time to get started.

Performance comparison of Java frameworks Performance comparison of Java frameworks Jun 04, 2024 pm 03:56 PM

According to benchmarks, for small, high-performance applications, Quarkus (fast startup, low memory) or Micronaut (TechEmpower excellent) are ideal choices. SpringBoot is suitable for large, full-stack applications, but has slightly slower startup times and memory usage.

How do the lightweight options of PHP frameworks affect application performance? How do the lightweight options of PHP frameworks affect application performance? Jun 06, 2024 am 10:53 AM

The lightweight PHP framework improves application performance through small size and low resource consumption. Its features include: small size, fast startup, low memory usage, improved response speed and throughput, and reduced resource consumption. Practical case: SlimFramework creates REST API, only 500KB, high responsiveness and high throughput

Golang framework documentation best practices Golang framework documentation best practices Jun 04, 2024 pm 05:00 PM

Writing clear and comprehensive documentation is crucial for the Golang framework. Best practices include following an established documentation style, such as Google's Go Coding Style Guide. Use a clear organizational structure, including headings, subheadings, and lists, and provide navigation. Provides comprehensive and accurate information, including getting started guides, API references, and concepts. Use code examples to illustrate concepts and usage. Keep documentation updated, track changes and document new features. Provide support and community resources such as GitHub issues and forums. Create practical examples, such as API documentation.

How to choose the best golang framework for different application scenarios How to choose the best golang framework for different application scenarios Jun 05, 2024 pm 04:05 PM

Choose the best Go framework based on application scenarios: consider application type, language features, performance requirements, and ecosystem. Common Go frameworks: Gin (Web application), Echo (Web service), Fiber (high throughput), gorm (ORM), fasthttp (speed). Practical case: building REST API (Fiber) and interacting with the database (gorm). Choose a framework: choose fasthttp for key performance, Gin/Echo for flexible web applications, and gorm for database interaction.

Java Framework Learning Roadmap: Best Practices in Different Domains Java Framework Learning Roadmap: Best Practices in Different Domains Jun 05, 2024 pm 08:53 PM

Java framework learning roadmap for different fields: Web development: SpringBoot and PlayFramework. Persistence layer: Hibernate and JPA. Server-side reactive programming: ReactorCore and SpringWebFlux. Real-time computing: ApacheStorm and ApacheSpark. Cloud Computing: AWS SDK for Java and Google Cloud Java.

What are the common misunderstandings in the learning process of Golang framework? What are the common misunderstandings in the learning process of Golang framework? Jun 05, 2024 pm 09:59 PM

There are five misunderstandings in Go framework learning: over-reliance on the framework and limited flexibility. If you don’t follow the framework conventions, the code will be difficult to maintain. Using outdated libraries can cause security and compatibility issues. Excessive use of packages obfuscates code structure. Ignoring error handling leads to unexpected behavior and crashes.

See all articles