I am still tired to translate these into Chinese.

but who cares? i write these posts just for myself

View

after a browser is done transforming the arkup's text to the DOM tree.
the AngularJs kicks in and traverses the parsed DOM structure.
each time it encounters a directive, AngularJs executes its logic to turn directives into

dynamc parts of the screen.
(AngularJs works using the live ,valid DOM tree. so do use HTML tags correctly.)

Declarative template view.
Angular promotes a declarative approach to UI construction.
in the past, if we want to change the UI, we already first defined a javascript function which tells

the logic and then invoke the function.
but in angular, the method is to add a directive to the DOM. what the directive does is to teach the browser
to do what we want to do.

let's image that we were asked to crate a form where user can type in a short message.
and them send it by clicking on a button. but there are some addtional requirements such as message size should be

limited to 100 characters and the Send button should be disabled if this limit is exceeded. a user should know how

many characters are left as they type. if the number of remaining characters is less than ten, the displayed number should
change the style to warn users. it should be possiable to clear text .
what will be do through our tranditional way like JQuery?
here is the code:
<form>
    <input id="message" type="text"/>
    <p>remaining <span id="remain"></span> </p>
    <input id="send" type="button" value="Send"/>
    <input id="clear" type="button" value="Clear"/>
</form>

$(document).ready(function(){
    $("#message").bind("change",function(){
        var messge=$(this).val();
        $("#remain").html(100-message.length);
        if(100-message.length<=0)
            $("#send").disable();
        else if (100-message.length<10)
            $("#remain").css("color","red");
        
    });
});
what we do in the javascript snippet is to find the DOM element and change its behavior.

now here is what we do in Angular:
<form action="" ng-controller="FormController">
        <input ng-model="message" ng-disabled="!hasValid()" />
        <p>remaining <span ng-class="{'text-warning':shouldWarn()}"> {{left()}}</span></p>
        <input type="button" value="Submit" ng-click='send()' ng-disabled="!hasValid()"/>
        <input type="button" value="Clear" ng-click="clear()"/>
</form>
function FormController($scope){
    $scope.message="";
    $scope.left=function(){
        return 10-$scope.message.length;
    }
    $scope.clear=function(){
        $scope.message="";
    }
    $scope.hasValid=function(){
        return $scope.left()>0;
    }
    $scope.shouldWarn=function(){
        return (100-$scope.message.length)<10;
    }
}
See the ng-XXX in the HTML tag. it just tells what we want to do with this tag.
in other words , we add new syntax to the HTML tag.
take ng-class as an example:
ng-class="{'text-warning':shouldWarn()}"   : if shouldWarn() return true, add class text-warning to the html tag.
maybe it's a little confusing.
but what do you mean when adding a attribute "style" to a specified element like :<div style="color:red;"></div>
what you want to do here is to add a display style to the div to make all characters' color to red.
then why can't we invent a new attribute 'ng-class' which means add class under special condition?
so take ng-class and all other ng-xxx directive the same as all existing attributes like "style" ,"length".
they just tell the browser what to do with the involved element.

AngularJs(Part 2)的更多相关文章

  1. 通过AngularJS实现前端与后台的数据对接(二)——服务(service,$http)篇

    什么是服务? 服务提供了一种能在应用的整个生命周期内保持数据的方法,它能够在控制器之间进行通信,并且能保证数据的一致性. 服务是一个单例对象,在每个应用中只会被实例化一次(被$injector实例化) ...

  2. AngularJs之九(ending......)

    今天继续angularJs,但也是最后一篇关于它的了,基础部分差不多也就这些,后续有机会再写它的提升部分. 今天要写的也是一个基础的选择列表: 一:使用ng-options,数组进行循环. <d ...

  3. AngularJS过滤器filter-保留小数,小数点-$filter

    AngularJS      保留小数 默认是保留3位 固定的套路是 {{deom | number:4}} 意思就是保留小数点 的后四位 在渲染页面的时候 加入这儿个代码 用来精确浮点数,指定小数点 ...

  4. Angular企业级开发(1)-AngularJS简介

    AngularJS介绍 AngularJS是一个功能完善的JavaScript前端框架,同时是基于MVC(Model-View-Controller理念的框架,使用它能够高效的开发桌面web app和 ...

  5. 模拟AngularJS之依赖注入

    一.概述 AngularJS有一经典之处就是依赖注入,对于什么是依赖注入,熟悉spring的同学应该都非常了解了,但,对于前端而言,还是比较新颖的. 依赖注入,简而言之,就是解除硬编码,达到解偶的目的 ...

  6. 步入angularjs directive(指令)--点击按钮加入loading状态

    今天我终于鼓起勇气写自己的博客了,激动与害怕并存,希望大家能多多批评指导,如果能够帮助大家,也希望大家点个赞!! 用angularjs 工作也有段时间了,总体感觉最有挑战性的还是指令,因为没有指令的a ...

  7. 玩转spring boot——结合AngularJs和JDBC

    参考官方例子:http://spring.io/guides/gs/relational-data-access/ 一.项目准备 在建立mysql数据库后新建表“t_order” ; -- ----- ...

  8. 玩转spring boot——结合jQuery和AngularJs

    在上篇的基础上 准备工作: 修改pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...

  9. 通过AngularJS实现前端与后台的数据对接(一)——预备工作篇

    最近,笔者在做一个项目:使用AngularJS,从而实现前端与后台的数据对接.笔者这是第一次做前端与后台的数据对接的工作,因此遇到了许多问题.笔者在这些问题中,总结了一些如何实现前端与后台的数据对接的 ...

  10. AngularJS 系列 学习笔记 目录篇

    目录: AngularJS 系列 01 - HelloWorld和数据绑定 AngularJS 系列 02 - 模块 (持续更新)

随机推荐

  1. Eclipse中执行Tomcat源代码

    1. 到http://archive.apache.org/dist/tomcat/tomcat-7下载Tomcat源码,本文用到的是apache-tomcat-7.0.19-src.zip: 注意: ...

  2. python 基础 8.0 regex 正则表达式--常用的正则表达式

    一. python 中常用的正则表达式         二. 正则表达式的网站,可以进行在线正则匹配 https://regex101.com/   1. 使用方法及正则介绍 1> ‘.’  匹 ...

  3. python 基础 2.2 if流程控制(二)

    一. if  else   1.逻辑值(bool)包含了两个值: ----True:表示非空的值,比如:string ,tuple,list,set,dictonary,所有非空的序列. -----F ...

  4. 【BZOJ2626】JZPFAR kd-tree+堆

    [BZOJ2626]JZPFAR Description 平面上有n个点.现在有m次询问,每次给定一个点(px, py)和一个整数k,输出n个点中离(px, py)的距离第k大的点的标号.如果有两个( ...

  5. MongoDB的CRUD操作(java Util )

    1.保存插入操作: public static synchronized String insert(DBObject record) { DBCollection col = MongoDB.get ...

  6. 在pycharm中误删了Python文件,怎么办,挺急的?

    1.今天在操作的时候,误删了文件,怎么办? (1)找到删除的上一级文件=>右键,找到Local History=>Show History (2),打开之后,双击左边的恢复时间内容,在右边 ...

  7. QQ登录集成到自己网站php代码(转载)

    我们现在在各大网站论坛都可以看到点击一个QQ图标就可以利用自己的QQ号在网站进行登录了,下面我来告诉你一段QQ登录集成到自己网站php代码,有需要的朋友可参考. 1.打开open.qq.com 添加创 ...

  8. SAP RFC 的介绍

    第一部分 RFC技术 什么是RFC? RFC是SAP系统和其他(SAP或非SAP)系统间的一个重要而常用的双向接口技术,也被视为SAP与外部通信的基本协议.简单地说,RFC过程就是系统调用当前系统外的 ...

  9. fastjson(转)

    博客地址 :http://blog.csdn.NET/shulianghan/article/details/41011605 fastjson 源码地址 : -- GitHub : https:// ...

  10. Docker与虚拟化

    核心知识点: 1.虚拟化的定义?虚拟化的核心和目标? 2.虚拟化的分类?Docker属于那种虚拟化? 3.Docker与传统虚拟化的区别?docker是直接在操作系统上实现虚拟化,直接复用本地操作系统 ...