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

寻∝梦
Release: 2018-09-07 14:02:45
Original
1212 people have browsed it

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template