angularJs学习笔记-入门

摘要:
在浏览器(3)源代码中导入角度文件(2)效果<你好{‘世界’}}<&lt:您的姓名;您好{{yourname | |'angular'}}<//t、 zoukankan.com/build/angular.min.js“charset=”utf-8“>hello;{eleName}}<DOCTYPEhtml>

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文件

  angularJs学习笔记-入门第1张

  (2)游览器里效果

  angularJs学习笔记-入门第2张

  (3)源码

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

4. ng-model

  (1)代码预览

  angularJs学习笔记-入门第3张

  (2)游览器里效果

  angularJs学习笔记-入门第4张

  (3)源码

  

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <script src="http://t.zoukankan.com/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>

 5.ng-controller

  (1)代码预览

  angularJs学习笔记-入门第5张

  (2)游览器效果

  angularJs学习笔记-入门第6张

  (3)源码

  

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>hello cynthia</title>
    <script src="http://t.zoukankan.com/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>

6.ng-repeat

  (1)代码预览

  angularJs学习笔记-入门第7张

  (2)游览器效果

  angularJs学习笔记-入门第8张

  (3)源码

  

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <script src="http://t.zoukankan.com/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>

7.example

  (1)代码预览

  angularJs学习笔记-入门第9张

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

  angularJs学习笔记-入门第10张

  (3)源码

  

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <script src="http://t.zoukankan.com/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>{{message}}</h3>
    </div>
  </body>
</html>

  

8.filters 过滤器

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

  (1)源码预览

  angularJs学习笔记-入门第11张

  (2)游览器中效果

  angularJs学习笔记-入门第12张

  (3)源码

  

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

9.利用filters做删选

  (1)代码预览

  angularJs学习笔记-入门第13张

  (2)游览器效果

  angularJs学习笔记-入门第14张

  (3)源码 

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <script src="http://t.zoukankan.com/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>

10.自定义filter

  (1)代码预览

  angularJs学习笔记-入门第15张

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

  angularJs学习笔记-入门第16张

  (3)源码

  

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>hello cynthia</title>
    <script src="http://t.zoukankan.com/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>

11.services

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

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

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

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

  (1)源码预览

  angularJs学习笔记-入门第17张

  (2)游览器里面效果

  angularJs学习笔记-入门第18张

  (3)源码

  

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>hello cynthia</title>
    <script src="http://t.zoukankan.com/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>

12.ng-show 和 ng-hide

  (1) 源码预览

  angularJs学习笔记-入门第19张

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

  angularJs学习笔记-入门第20张

  (3)源码

  

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
    <script src="http://t.zoukankan.com/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>showing {{shouldShow}}</h3>
      </div>
      <div class="" ng-hide='shouldShow'>
        <h3>hiding {{shouldShow}}</h3>
      </div>
    </div>
  </body>
</html>

13.

免责声明:文章转载自《angularJs学习笔记-入门》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇C#遍历指定文件夹中的所有文件及操作Sqlserver 标识列ID在两个表之间避免重复之生成奇、偶数下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

RequestBodyAdvice和ResponseBodyAdvice详解,@ControllerAdvice注解

一、源码解析 这是spring 4.2新加的两个接口 1、RequestBodyAdvice public interface RequestBodyAdvice { boolean supports(MethodParameter var1, Type var2, Class<? extends HttpMessageConverter&l...

centos7搭建DVWA环境

///首先先下载好dvwa这个压缩包,去晚上搜一下就有了,话不多说,开始干活 第一步: #yum install -y mariadb* php* httpd安装好数据库,php和apache 第二步: 然后将下载好的DVWA-master.zip解压#unzip DVWA-master.zip //解压 #mv DVWA-master.zip DVWA...

最全Pycharm教程(10)——Pycharm调试器总篇

  最全Pycharm教程(1)——定制外观   最全Pycharm教程(2)——代码风格   最全Pycharm教程(3)——代码的调试、执行   最全Pycharm教程(4)——有关Python解释器的相关配置   最全Pycharm教程(5)——Python快捷键相关设置   最全Pycharm教程(6)——将Pycharm作为Vim编...

Grub4dos安装和启动完全指南

GRUB4DOS的启动方式有很多种,介绍如下: 1.从MBR中启动 把GRUB4DOS启动代码安装到MBR后,开机后便能直接进入GRUB4DOS。 安装到MBR需要用到工具bootlace或grubinst, bootlace可运行在DOS,Windows 95/98/Me 和 Linux下,而grubinst可运行在 Windows NT/2000/X...

Bmob云IM实现头像更换并存入Bmob云数据库中(1.拍照替换,2.相册选择)

看图效果如下: 1.个人资料界面 2.点击头像弹出对话框 3.点击拍照 4.切割图片,选择合适的部分   5.点击保存,头像替换完毕,下面看从相册中选择图片。 6.点击相册 7.任选一张图片 8.切割图片  9.图片替换成功 亲测退出账户后重新登陆或者换模拟器登陆有效!!! 图片已经上传到云端了!!! 下面先上xml代码: 里面出现的可能报...

12mybatis调用执行存储过程

mybatis 调用执行存储过程 mysql 声明建立存储过程 删除 在mysql中调用 -- 声明定义存储过程 delimiter $$ create procedure delbook(id int) begin delete from book where book_id=id; end$$ delimiter ; -- 删除存储过程 drop p...