Home  >  Article  >  Web Front-end  >  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

寻∝梦
寻∝梦Original
2018-09-08 14:14:551171browse

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>
  

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");
        });            
    }
});

上面示例中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>

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", []);

 如果希望在模块中添加元素,你可以通过名称调用模块向其中添加。例如: 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 :
// 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 会自动添加需要的引用:

...

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

这篇文章中我们了解了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:
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