Table of Contents
{{message}}
showing {{shouldShow}}
hiding {{shouldShow}}
Home Web Front-end HTML Tutorial angularJs学习笔记-入门_html/css_WEB-ITnose

angularJs学习笔记-入门_html/css_WEB-ITnose

Jun 24, 2016 am 11:26 AM

1.angularJs简介

  angularJs是一个MV*的javascript框架(Model-View-Whatever,不管是MVVM还是MVC,统归MDV(model drive view)),其实是由google推出的SPA(single-page-application)应用框架。它的用于 数据在后端和前端之间的双向绑定。这就意味着你在后台改变数据,这些变动立刻就会出现在view上。

  在加载的时候,angular会将你的dom树和javascript转向一个angular app 。包含着angular指令和过滤器的html会被编译成一个树图,响应的范围和控制器会被附加在这个树上,内部的应用循环确保了视图和模型之间的数据绑定。每次模型被更新(可以通过ajax请求,也可以直接操作控制器),angular会重新运行它的 $digest循环,跟新数据绑定确保所有东西是同步的。

  js代码是用一种命令的方式操作dom。而在angular中,直接操作dom是不被提倡的。dom由视图管理,data在scope中,方法在控制器里。

 

3. ng-app

  (1)代码预览,引入angular文件

  

  (2)游览器里效果

  

  (3)源码

<!DOCTYPE html><html>  <head>    <meta charset="utf-8">    <title></title>    <script src="../build/angular.min.js" charset="utf-8"></script>  </head>  <body>      <div class="" ng-app>        hello{{'world'}}      </div>  </body></html>
Copy after login

4. ng-model

  (1)代码预览

  

  (2)游览器里效果

  

  (3)源码

  

<!DOCTYPE html><html>  <head>    <meta charset="utf-8">    <title></title>    <script src="../build/angular.min.js" charset="utf-8"></script>  </head>  <body>    <div class="" ng-app>      your name :      <input type="text" name="name" value="" ng-model="yourname" placeholder="angular">      <hr>      hello {{yourname || 'angular'}}    </div>  </body></html>
Copy after login

5.ng-controller

  (1)代码预览

  

  (2)游览器效果

  

  (3)源码

  

<!DOCTYPE html><html>  <head>    <meta charset="utf-8">    <title>hello cynthia</title>    <script src="../build/angular.min.js" charset="utf-8"></script>  </head>    <script type="text/javascript">    var app = angular.module('app',[]);    app.controller('helloCynthia',function($scope){      $scope.eleName = "cynthia"    })  </script>    <body>    <div class="" ng-app='app' ng-controller='helloCynthia'>      hello,{{eleName}}    </div>  </body></html>
Copy after login

6.ng-repeat

  (1)代码预览

  

  (2)游览器效果

  

  (3)源码

  

<!DOCTYPE html><html>  <head>    <meta charset="utf-8">    <title></title>    <script src="../build/angular.min.js" charset="utf-8"></script>  </head>  <script type="text/javascript">    var app = angular.module('ngrepeat',[])    app.controller('ngrepeatCtr',function($scope){      $scope.developers=[        {name:'wuqian',country:'china'},        {name:'cynthia',country:'usa'},        {name:'wupore',country:'canada'},        {name:'selene',country:'english'}      ]    })  </script>  <body>    <div class="" ng-app='ngrepeat' ng-controller='ngrepeatCtr'>      <ul>        <li ng-repeat='person in developers'>          {{person.name}} from {{person.country}}        </li>      </ul>    </div>  </body></html>
Copy after login

7.example

  (1)代码预览

  

  (2)游览器效果 (用户在input里输入后,点击button,在下方显示输入,但是目前显示undefined。。。) 

  

  (3)源码

  

<!DOCTYPE html><html>  <head>    <meta charset="utf-8">    <title></title>    <script src="../build/angular.min.js" charset="utf-8"></script>  </head>  <script type="text/javascript">    var app = angular.module('appName',[]);    app.controller('controllerName',function($scope){      $scope.clickName = function(){        $scope.message = 'Name : ' + $scope.userInput;      }    })  </script>  <body>    <div class="" ng-app='appName' ng-controller='controllerName'>      <p>        what's your name ?      </p>      <br>      <input type="text" name="name" placeholer="input your name here" ng-model=‘userInput’>      <button type="button" name="button" ng-click='clickName()'>click here</button>      <h3 id="message">{{message}}</h3>    </div>  </body></html>
Copy after login

 

8.filters 过滤器

  angular提供的过滤器和unix中的管道pipeline相似。比如我们要在网页中显示价格$可以这样写

  (1)源码预览

  

  (2)游览器中效果

  

  (3)源码

  

<!DOCTYPE html><html>  <head>    <meta charset="utf-8">    <title></title>    <script src="../build/angular.min.js" charset="utf-8"></script>  </head>  <body>      <div class="" ng-app>        <span>iphone : {{63573 | currency}}</span>      </div>  </body></html>
Copy after login

9.利用filters做删选

  (1)代码预览

  

  (2)游览器效果

  

  (3)源码 

<!DOCTYPE html><html>  <head>    <meta charset="utf-8">    <title></title>    <script src="../build/angular.min.js" charset="utf-8"></script>  </head>  <script type="text/javascript">    var app = angular.module('ngrepeat',[])    app.controller('ngrepeatCtr',function($scope){      $scope.developers=[        {name:'wuqian',country:'china'},        {name:'cynthia',country:'usa'},        {name:'wupore',country:'canada'},        {name:'selene',country:'english'}      ]    })  </script>  <body>    <div class="" ng-app='ngrepeat' ng-controller='ngrepeatCtr'>      <input type="text" name="name" value="" ng-model='search'>      <ul>        <li ng-repeat='person in developers | filter:search'>          {{person.name}} from {{person.country}}        </li>      </ul>    </div>  </body></html>
Copy after login

10.自定义filter

  (1)代码预览

  

  (2)游览器效果(首字母变大写)

  

  (3)源码

  

<!DOCTYPE html><html>  <head>    <meta charset="utf-8">    <title>hello cynthia</title>    <script src="../build/angular.min.js" charset="utf-8"></script>  </head>  <script type="text/javascript">    var app = angular.module('app',[]);    // 自定义filter    app.filter('capt',function(){      return function(input,param){        return input.substring(0,1).toUpperCase()+input.substring(1);      }    })  </script>  <body>    <div class="" ng-app='app'>      <span>{{'this is some text' | capt}}</span>    </div>  </body></html>
Copy after login

11.services

  在controller之间共享数据对我们是很有用的,但是每个controller都有自己的scope,所以我们不能将其绑定到其他的controller上。为此angular提供了的解决方案是services。

  angular内置了很多services,比如http请求、异步promises编程模式。这也是angular的核心(依赖注入)的关键。

  services都是单例的,也就是说在一个应用里,每个service对象只会被实例化一次。它主要负责提供一个接口把特定的函数需要的方法放在一起。最常见的方法是angular.module API的factory方式:

  例子:通过services实现oneCtrl和twoCtrl之间共享一个数据 user

  (1)源码预览

  

  (2)游览器里面效果

  

  (3)源码

  

<!DOCTYPE html><html>  <head>    <meta charset="utf-8">    <title>hello cynthia</title>    <script src="../build/angular.min.js" charset="utf-8"></script>  </head>  <script type="text/javascript">    var app = angular.module('app',[]);    // 通过services实现oneCtrl和twoCtrl之间共享一个数据 user    app.factory('userInfor',function(){      var user={        name : 'Angular.js'      }      return user;    })    app.controller('oneCtrl',function($scope,userInfor){      $scope.user = userInfor;    })    app.controller('twoCtrl',function($scope,userInfor){      $scope.user = userInfor;    })  </script>  <body>    <div class="" ng-app='app'>      <div class="" ng-controller='oneCtrl'>        oneCtrl :        <input type="text" name="name" value="" ng-model='user.name'>      </div>      <div class="" ng-controller='twoCtrl'>        twoCtrl :        <input type="text" name="name" value="" ng-model='user.name'>      </div>    </div>  </body></html>
Copy after login

12.ng-show 和 ng-hide

  (1) 源码预览

  

  (2)游览器里效果:点击按钮下面的内容出现/隐藏

  

  (3)源码

  

<!DOCTYPE html><html>  <head>    <meta charset="utf-8">    <title></title>    <script src="../build/angular.min.js" charset="utf-8"></script>  </head>  <body>    <div class="" ng-app>      <button type="button" name="button" ng-init='shouldShow=true' ng-click='shouldShow = !shouldShow'>Flip the shouldShow variable</button>      <div class="" ng-show='shouldShow'>        <h3 id="showing-shouldShow">showing {{shouldShow}}</h3>      </div>      <div class="" ng-hide='shouldShow'>        <h3 id="hiding-shouldShow">hiding {{shouldShow}}</h3>      </div>    </div>  </body></html>
Copy after login

 

 

13.

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)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

How to implement adaptive layout of Y-axis position in web annotation? How to implement adaptive layout of Y-axis position in web annotation? Apr 04, 2025 pm 11:30 PM

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

See all articles