Home Web Front-end JS Tutorial How much do you know about the basic introduction to angularjs? Here is a detailed introduction to angularjs

How much do you know about the basic introduction to angularjs? Here is a detailed introduction to angularjs

Sep 07, 2018 pm 02:02 PM
angularjs

This article introduces a brief intermediate article about angularjs, introduces single-page web applications, three template methods,$scope, Scope, Traversal, Other instructions, Request datajqLite, $watch, $apply. Next let us read this article together

Characteristics of single-page applications: The entire website consists of one page, the public part is only loaded once, and Ajax partial refresh is used to achieve the purpose of page switching. When the page jumps to a white screen, the anchor point and the page correspond to the single-page web application.

Scenario: Single-page applications are not friendly to search engines and are not suitable for public display websites. Website back-end management systems, Office OA, hybrid apps and other applications that do not need to be searched by search engines

<script src="node_modules/angular/angular.min.js"></script>
  <script src="node_modules/angular-route/angular-route.min.js"></script>
  <body ng-app="myApp">
      <a href="#!/index">首页</a>
        <a href="#!/list">列表页</a>
        <div ng-view></div>
  </body>
  <script>
      var app=angular.module(&#39;myApp&#39;,[&#39;ngRoute&#39;])
      app.config(function($routeProvider){
          $routeProvider
              .when(&#39;/index&#39;,{
                  templateUrl:&#39;./tpl/index.html&#39;,
                    controller:&#39;indexCtrl&#39;
              })
                .when(&#39;/list&#39;,{
                  templateUrl:&#39;./tpl/list.html&#39;,
                    controller:&#39;listCtrl&#39;
              })
                .otherwise(&#39;/index&#39;)
      });
        app.controller(&#39;indexCtrl&#39;,function($scope){
          $scope.msg="我是首页msg"
      })
        app.controller(&#39;listCtrl&#39;,function($scope){
          $scope.msg="我是列表页msg"
      })  </script>
Copy after login

Three template methods

<script>
    templateUrl:&#39;./tpl/index.html&#39;//localhost
      template:&#39;<div>我是首页</div>&#39;//file|localhosst
      template:&#39;indexTpl&#39;//file|localhosst</script>
Copy after login

$scope

There are many parameters that can be passed, there is no need to write them out one by one

You cannot rely on passing parameters in angularjs The order is the name

If the formal parameter name changes, angularjs will not know what to do

Solution: write an array as the second parameter, and put the callback function in the array

During compression, the string will not be compressed, so the string is passed in the array to determine the order of parameters

 <script>
      angular.module("myApp",[]).controller("demoCtrl",["$scope","$timeout","$http","$location",function(a,b,c,d){
          a.msg="我是msg"
      }])  </script>
Copy after login

Scope

Function Domain proximity principle

The area controlled by the controller in angularjs is a local scope,

That is, $scope represents the local scope

$rootScope represents the global scope Domain

Variables are first searched along $scope. If not found, search globally.

You can mount public attribute methods

Traverse

ng-repeat="Current item in data during loop" loops data and generates current DOM element

 <ul>
      <li ng-repeat="item in arr">{{item}}</li>
  </ul>
Copy after login

Traverse array objects, can be nested, with ng-repeat Tags of ng-repeat can also be nested



  • {{person.name}}{{person.age}}
    {{item}}


数组项重复,会报错

 <ul>
      <li ng-repeat="item in arr track by $index">{{item}}</li>
  </ul>
Copy after login

其他指令

ng-class="{'类名1':布尔值,'类名2':布尔值}"专门用来添加或者删除类名,接收的值是一个对象,布尔值为真,添加类名,布尔值为假,删除类名

复选框,ng-model用来获取复选框的值,是一个布尔值

ng-bind="数据",将msg放到属性中进行加载,避免出现闪烁效果

ng-bind-template="{{数据1}} {{数据2}}"

ng-non-bindable直接得到插值表达式中的内容,只要与属性相关,都不执行

ng-show="布尔值",控制元素的显示和隐藏

ng-hide="布尔值",控制元素的显示和隐藏

ng-if="布尔值",控制元素的显示和隐藏 true 显示 false 隐藏

ng-switch&ng-switch-when用法和switch-case类似

事件指令

onclick => ng-click

onmouseenter => ng-mouseenter

onchange => ng-change

ng-dblclick 双击事件

ng-src没有src就不会解析就不会报错,直到angularjs加载完成,解析ng-src之后再生成src

ng-href同上

ng-options用来循环下拉列表,不能单独使用,需要配合ng-model一起使用

请求数据

要请求数据需要先引入js文件

引入的js文件作为依赖文件,控制器中必须写入$http

$http-->请求的地址,相当于jQuery中的ajax

method-->type请求的方式

params-->data只用于get传参

data可以用于post传参

$http点then后面是两个回调函数

第一个回调函数是成功回调

第二个回调函数是失败回调

res是形参,表示请求回来的数据

 <script src="node_modules/angular/angular.js"></script>
  <script src="node_modules/angular-sanitize.min.js"></script>
  <script>
      angular.module(&#39;myApp&#39;,[&#39;ngSanitize&#39;])
            .controller(&#39;demoCtrl&#39;,[&#39;$scope&#39;,&#39;$http&#39;,function($scope,$http){
              $http({                  url:&#39;./test.json&#39;,                    method:&#39;post&#39;,//请求方式
                    params:{//get传参
                        a:1,                      b:2
                    },                    data:{                        c:3,                      d:4
                    }
              }).then(function(res){                    //成功回调
                  console.log(res)
              },function(){                  //失败回调
              })                //另外一种写法
                $http.get(&#39;./test.json&#39;,{params:{a:1,b:2}}).then(function(res){                    //get方式传参
                  console.log(res)
              })
                $http.post(&#39;./test.json&#39;,{c:3,d:4}.then(function(res){                    //post方式传参
                  console.log(res)
              })
          }])  </script>
Copy after login

jqLite

为了方便DOM操作,angularjs提供了一个jQuery精简版,叫做jqLite

$(原生的JS对象)将原生JS对象转换成jQuery对象,目的是为了使用jQuery对象下面提供的方法

angularjs.element(原生JS对象)将原生JS对象转换成jqLite对象,目的是为了使用jqLite对象下面提供的方法

这里angularjs.element相当于jQuery中的$

jqLite中方法的使用和jQuery高度相似

$watch

$watch用来监控数据的变化

第一个参数是要监控的数据,第二个参数是回调函数

回调函数中第一个参数newValue是用户输入的最新内容,第二个参数oldValue是上一次用户输入的内容

页面一上来的时候,回调函数会先执行一次

<script>
      $scope.$watch(&#39;val&#39;,function(newValue,oldValue){          if(newValue.length>10){
              $scope.tips="大于10";
          }else{
              $scope.tips="小于10"
          }
      })  </script>
Copy after login

$apply

当通过原生JS将angularjs中的数据做了改变以后,angularjs不知道,所以需要调用$apply()方法通知angularjs更新html页面

以上就本篇关于angularjs简历的中级篇文章了,下一篇终极的在后面,大家期待吧,想学更多关于angularjs的相关知识就到PHP中文网angularjs参考手册栏目中学习。

The above is the detailed content of How much do you know about the basic introduction to angularjs? Here is a detailed introduction to angularjs. 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

The latest 5 angularjs tutorials in 2022, from entry to mastery The latest 5 angularjs tutorials in 2022, from entry to mastery Jun 15, 2017 pm 05:50 PM

Javascript is a very unique language. It is unique in terms of the organization of the code, the programming paradigm of the code, and the object-oriented theory. The issue of whether Javascript is an object-oriented language that has been debated for a long time has obviously been There is an answer. However, even though Javascript has been dominant for twenty years, if you want to understand popular frameworks such as jQuery, Angularjs, and even React, just watch the "Black Horse Cloud Classroom JavaScript Advanced Framework Design Video Tutorial".

Use PHP and AngularJS to build a responsive website to provide a high-quality user experience Use PHP and AngularJS to build a responsive website to provide a high-quality user experience Jun 27, 2023 pm 07:37 PM

In today's information age, websites have become an important tool for people to obtain information and communicate. A responsive website can adapt to various devices and provide users with a high-quality experience, which has become a hot spot in modern website development. This article will introduce how to use PHP and AngularJS to build a responsive website to provide a high-quality user experience. Introduction to PHP PHP is an open source server-side programming language ideal for web development. PHP has many advantages, such as easy to learn, cross-platform, rich tool library, development efficiency

Build web applications using PHP and AngularJS Build web applications using PHP and AngularJS May 27, 2023 pm 08:10 PM

With the continuous development of the Internet, Web applications have become an important part of enterprise information construction and a necessary means of modernization work. In order to make web applications easy to develop, maintain and expand, developers need to choose a technical framework and programming language that suits their development needs. PHP and AngularJS are two very popular web development technologies. They are server-side and client-side solutions respectively. Their combined use can greatly improve the development efficiency and user experience of web applications. Advantages of PHPPHP

Build a single-page web application using Flask and AngularJS Build a single-page web application using Flask and AngularJS Jun 17, 2023 am 08:49 AM

With the rapid development of Web technology, Single Page Web Application (SinglePage Application, SPA) has become an increasingly popular Web application model. Compared with traditional multi-page web applications, the biggest advantage of SPA is that the user experience is smoother, and the computing pressure on the server is also greatly reduced. In this article, we will introduce how to build a simple SPA using Flask and AngularJS. Flask is a lightweight Py

Use PHP and AngularJS to develop an online file management platform to facilitate file management Use PHP and AngularJS to develop an online file management platform to facilitate file management Jun 27, 2023 pm 01:34 PM

With the popularity of the Internet, more and more people are using the network to transfer and share files. However, due to various reasons, using traditional methods such as FTP for file management cannot meet the needs of modern users. Therefore, establishing an easy-to-use, efficient, and secure online file management platform has become a trend. The online file management platform introduced in this article is based on PHP and AngularJS. It can easily perform file upload, download, edit, delete and other operations, and provides a series of powerful functions, such as file sharing, search,

Introduction to the basics of AngularJS Introduction to the basics of AngularJS Apr 21, 2018 am 10:37 AM

The content of this article is about the basic introduction to AngularJS. It has certain reference value. Now I share it with you. Friends in need can refer to it.

How to use AngularJS in PHP programming? How to use AngularJS in PHP programming? Jun 12, 2023 am 09:40 AM

With the popularity of web applications, the front-end framework AngularJS has become increasingly popular. AngularJS is a JavaScript framework developed by Google that helps you build web applications with dynamic web application capabilities. On the other hand, for backend programming, PHP is a very popular programming language. If you are using PHP for server-side programming, then using PHP with AngularJS will bring more dynamic effects to your website.

How to use PHP and AngularJS for front-end development How to use PHP and AngularJS for front-end development May 11, 2023 pm 05:18 PM

With the popularity and development of the Internet, front-end development has become more and more important. As front-end developers, we need to understand and master various development tools and technologies. Among them, PHP and AngularJS are two very useful and popular tools. In this article, we will explain how to use these two tools for front-end development. 1. Introduction to PHP PHP is a popular open source server-side scripting language. It is suitable for web development and can run on web servers and various operating systems. The advantages of PHP are simplicity, speed and convenience

See all articles