学习要点:

  • 数据绑定
    • 单向绑定
      • 内联模块表达式 {{data}}
      • ng-bind指令
      • 模板绑定 ng-bind-template
    • 阻止绑定
    • 双向绑定
  • 模板指令
    • 重复生成元素 ng-repeat
      • 重复操作对象属性
      • 使用数据对象的键值进行工作
      • 使用内置变量工作
    • 重复生成多个顶层元素
    • 局部视图 ng-include
    • 有条件的交互元素 ng-switch
    • 隐藏未处理的内联模板绑定表达式

JQ将HTML文档当作一个需要解决的问题,而NG则将HTML当作构建Web程序的基础。
NG有超过50个内置指令,同时你也可以自定义指定来增强程序的能力。
为什么要使用指令?
指令暴露了NG的核心功能,你可以只使用指令就完成你需要的功能
何时使用指令?
在NG的程序的各个部分都OK!

NG提供了许多内置的绑定数据和模板的指令,这里我们先预览一下:

接下来,我们将通过具体的例子来讲解其中大部分指令的用法和意思
准备项目代码

<!DOCTYPE>
<!-- use module -->
<html ng-app="exampleApp">
<head>
    <title>Angular Directive</title>
    <Meta charset="utf-8"/>
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css">
</head>
<body>
<div id="todoPanel" class="panel" ng-controller="defaultCtrl">
    <h3 class="panel-header">To Do List</h3>
</div>
<script type="text/javascript" src="js/angular.min.js"></script>
<script type="text/javascript"> // define a module named exampleApp angular.module("exampleApp",[]) // define a controller named defaultCtrl .controller('defaultCtrl',function ($scope) { // todo list $scope.todos = [ { action : 'play ball',complete : false },{ action : 'runnging',{ action : 'eating',complete : true },{ action : 'shopping',complete : false } ]; }) </script>
</body>
</html>

一、使用数据绑定指令
1.单向绑定
方式一:内联模块表达式

<div>There are {{todos.length}} items</div>

方式二:ng-bind指令

<div>There are <span ng-bind='todos.length'></span> items</div>

方式三:ng-bind-template模板绑定

<div ng-bind-template="First : {{todos[0].action}} . Second : {{todos[1].action}}."></div>

2.阻止内联数据绑定—ng-no-bindable

<div ng-non-bindable>{{todos[3].action}}</div>

3.双向绑定—ng-model

<div class="well">
    <div>The first item is : {{todos[0].action}}</div>
</div>
<div class="form-group well">
    <label for="firstItem">Set First Item : </label>
    <input name="firstItem" class="form-control" ng-model="todos[0].action"/>
</div>

在你修改input中的值时,你会发现well中的值也在改变
二、使用模板指令
1.重复生成元素—ng-repeat

<table class="table table-striped table-bordered"> <thead> <tr><th>Action</th><th>Done</th></tr> </thead> <tbody> <!-- 重复生成元素 --> <!-- <tr ng-repeat="item in todos"> <td>{{item.action}}</td> <td>{{item.complete}}</td> </tr> --> <!-- 重复操作对象属性 --> <!-- <tr ng-repeat="item in todos"> <td ng-repeat="prop in item">{{prop}}</td> </tr> --> <!-- 使用数据对象的键值工作 --> <!-- <tr ng-repeat="item in todos"> <td ng-repeat="(key,value) in item"> {{key}} = {{value}} </td> </tr>--> <!-- 使用内置的变量$index,另外还有$first、$last等--> <tr ng-repeat="item in todos"> <td>{{$index + 1}}</td> <td ng-repeat="(key,value) in item"> {{key}} = {{value}} </td> </tr> </tbody> </table>

2.重复生成多个顶层元素

<table class="table table-striped table-bordered"> <thead> <tr><th>#</th><th>Action</th><th>Done</th></tr> </thead> <tbody> <tr ng-repeat-start="item in todos"> <td>This is item {{$index}}</td> </tr> <tr> <td>The action is : {{item.action}}</td> </tr> <tr ng-repeat-end> <td>Item {{$index}} is {{item.complete ? '' : "not "}} complete</td> </tr> </tbody> </table> 

3.局部视图 —ng-include
先定义table.html视图

<table class="table table-striped table-bordered"> <thead> <tr><th>#</th><th>Action</th><th>Done</th></tr> </thead> <tbody> <tr ng-repeat="item in todos"> <td>{{$index + 1}}</td> <td ng-repeat="value in item"> {{value}} </td> </tr> </tbody> </table>

接着引入table.html视图

<ng-include src="'table.html'"></ng-include>

动态引入局部视图
我们需要动态引入table.html和lilst.html—需要定义
定义list.html视图

<ol> <li ng-repeat="item in todos"> {{item.action}} <span ng-if="item.complete"> (Done)</span> </li> </ol>

动态指定视图

$scope.viewFile = function () {
    return $scope.showList ? "list.html" : "table.html";
}

当勾选复选框时,使用list.html视图,反而反之;

<div class="well">
    <div class="checkBox">
        <label>
            <input type="checkBox" ng-model="showList"> Use the list view
        </label>
    </div>
</div>
<ng-include src="viewFile()"></ng-include>

4.将ng-include指令当作属性使用
其他部分不变

<div ng-include="viewFile()"></div>

5.有条件的交换元素— ng-switch,类似也JS的switch语句

<div class="well">
    <div class="radio" ng-repeat="button in ['None','Table','List']">
        <label>
            <input type="radio" ng-model="data.mode" value="{{button}}" ng-checked="$first"/>{{button}}
        </label>
    </div>
</div>
<div ng-switch on="data.mode">
    <div ng-switch-when="Table">
        <ng-include src="'table.html'"></ng-include>
    </div>
    <div ng-switch-when="List">
        <ng-include src="'list.html'"></ng-include>
    </div>
    <div ng-switch-default>
        Select another option to display a layout
    </div>
</div>

6.隐藏未处理的内联模块绑定表达式
在我们使用内联模块表达式{{}}时,可能会出现{{data}}类似的一闪而过的页面,这是我们需要对其隐藏

<div class="well" ng-cloak>
    <div class="radio" ng-repeat="button in ['None','List']">
        <label>
            <input type="radio" ng-model="data.mode" value="{{button}}" ng-checked="$first"/>{{button}}
        </label>
    </div>
</div>
<div ng-switch on="data.mode" ng-cloak>
    <div ng-switch-when="Table">
        <ng-include src="'table.html'"></ng-include>
    </div>
    <div ng-switch-when="List">
        <ng-include src="'list.html'"></ng-include>
    </div>
    <div ng-switch-default>
        Select another option to display a layout
    </div>
</div>

AngularJS 使用绑定和模板指令的更多相关文章

  1. Swift 字符串替换/过滤/切割/拼接

    替换为/结果过滤过滤掉单个字符/结果过滤掉开头和结尾的空白结果切割对字符串使用/作为分隔符来切割,不允许空字符串使用split函数结果是一个数组对字符串使用/作为分隔符来切割,允许空字符串结果拼接结果

  2. Swift开发教程--字符串的操作

    替换把?替换为/结果

  3. 第七章:table单元格的选择和UIAlertController

    运行app,自己试试选择cell更多关于UIAlertController再继续研究之前,我们需要更多的了解一下UIAlertController。UIAlertController是在iOS8引入用来替代UIAlertView和UIActionSheet的。参照上面的代码片段,我们可以指定UIAlertController的preferredStyle。创建好动作后可以使用addAction将动作和UIAlertController连接起来。这就是使用UIAlertController的方法。

  4. tableview使用自定义类,页面跳转,本地存储

    如图,添加下面的三行代码2下面创建自己的cell,新建一个swift文件,命名为TableViewCell3因为还要考虑到界面的跳转,需要新建swift文件PushTest

  5. Swift UITableView相关功能八

    但是,我们发现当我们点击右侧索引的时候好像和table的关系不明确。其实我们少了一个代理方法,他是专门用来关联索引和table分区的这里我们简单设置了一下,将字母顺序和table的分区对应上了。

  6. Swift设置Table View的Cell中Lable自适应内容高度的

    Swift设置TableView的Cell中Lable自适应内容高度的最后修改在TableCell中Label的lines属性,将其设置为0。

  7. 更加 Swift 化的 Collection View 和 Table View Cells

    本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请发送邮件至dio@foxmail.com举报,一经查实,本站将立刻删除。

  8. swift 网络搜索热词排行

    1.使用www.showapi.com上的接口,需要注册添加一个App,这样才能获取appid和secret密钥,调用前需要订购套餐(选免费的就可以了);2.外部库Podfile文件内容,SnapKit这里暂时不需要用到:3.桥接头文件参考:http://www.jb51.cc/article/p-pcleyxep-te.html4.AppTransportSecurityhasblockedac

  9. 制作一个可以滑动操作的 Table View Cell

    本教程将会向你展示如何制作一个这样的TableViewCell,而不用因嵌套的ScrollView陷入困境。如果你还不知道一个可滑动的TableViewCell意味着什么,那么看看Apple的邮件应用:可能你会想,既然Apple展示了这种方案,那它应该已将其开放给开发者使用了。这会要求你深入研究iOS7UITableViewCell的结构,以便复制出我们需要的行为。打开MasterViewController.m并找到viewDidLoad。这个循环添加了一些字符串到_objects数组,应用运行时,这些

  10. 如何实现可收起和展开的Table Section

    如何实现可收起和展开的TableSection这是一个简单的iOSswift项目,旨在介绍如何实现可收起和展开的tablesection,并且,项目不需要mainstoryboard,XIB,注册nib等,只需要纯的Swfit代码!切换收起和展开的函数如下:注意到我们不是简单的重绘整个section,实际上我们只需要重绘section里的所有cell就好,这样做的好处是避免了sectionheader因重绘时闪烁的效果,最重要是的可以让我们更平滑地处理我们想要的动画效果,例如旋转那个箭头,改变背景颜色等等

随机推荐

  1. Angular2 innerHtml删除样式

    我正在使用innerHtml并在我的cms中设置html,响应似乎没问题,如果我这样打印:{{poi.content}}它给了我正确的内容:``但是当我使用[innerHtml]=“poi.content”时,它会给我这个html:当我使用[innerHtml]时,有谁知道为什么它会剥离我的样式Angular2清理动态添加的HTML,样式,……

  2. 为Angular根组件/模块指定@Input()参数

    我有3个根组件,由根AppModule引导.你如何为其中一个组件指定@input()参数?也不由AppModalComponent获取:它是未定义的.据我所知,你不能将@input()传递给bootstraped组件.但您可以使用其他方法来做到这一点–将值作为属性传递.index.html:app.component.ts:

  3. angular-ui-bootstrap – 如何为angular ui-bootstrap tabs指令指定href参数

    我正在使用角度ui-bootstrap库,但我不知道如何为每个选项卡指定自定义href.在角度ui-bootstrap文档中,指定了一个可选参数select(),但我不知道如何使用它来自定义每个选项卡的链接另一种重新定义问题的方法是如何使用带有角度ui-bootstrap选项卡的路由我希望现在还不算太晚,但我今天遇到了同样的问题.你可以通过以下方式实现:1)在控制器中定义选项卡href:2)声明一个函数来改变控制器中的散列:3)使用以下标记:我不确定这是否是最好的方法,我很乐意听取别人的意见.

  4. 离子框架 – 标签内部的ng-click不起作用

    >为什么标签标签内的按钮不起作用?>但是标签外的按钮(登陆)工作正常,为什么?>请帮我解决这个问题.我需要在点击时做出回复按钮workingdemo解决方案就是不要为物品使用标签.而只是使用divHTML

  5. Angular 2:将值传递给路由数据解析

    我正在尝试编写一个DataResolver服务,允许Angular2路由器在初始化组件之前预加载数据.解析器需要调用不同的API端点来获取适合于正在加载的路由的数据.我正在构建一个通用解析器,而不是为我的许多组件中的每个组件设置一个解析器.因此,我想在路由定义中传递指向正确端点的自定义输入.例如,考虑以下路线:app.routes.ts在第一个实例中,解析器需要调用/path/to/resourc

  6. angularjs – 解释ngModel管道,解析器,格式化程序,viewChangeListeners和$watchers的顺序

    换句话说:如果在模型更新之前触发了“ng-change”,我可以理解,但是我很难理解在更新模型之后以及在完成填充更改之前触发函数绑定属性.如果您读到这里:祝贺并感谢您的耐心等待!

  7. 角度5模板形式检测形式有效性状态的变化

    为了拥有一个可以监听其包含的表单的有效性状态的变化的组件并执行某些组件的方法,是reactiveforms的方法吗?

  8. Angular 2 CSV文件下载

    我在springboot应用程序中有我的后端,从那里我返回一个.csv文件WheniamhittingtheURLinbrowsercsvfileisgettingdownloaded.现在我试图从我的角度2应用程序中点击此URL,代码是这样的:零件:服务:我正在下载文件,但它像ActuallyitshouldbeBook.csv请指导我缺少的东西.有一种解决方法,但您需要创建一个页面上的元

  9. angularjs – Angular UI-Grid:过滤后如何获取总项数

    提前致谢:)你应该避免使用jQuery并与API进行交互.首先需要在网格创建事件中保存对API的引用.您应该已经知道总行数.您可以使用以下命令获取可见/已过滤行数:要么您可以使用以下命令获取所选行的数量:

  10. angularjs – 迁移gulp进程以包含typescript

    或者我应该使用tsc作为我的主要构建工具,让它解决依赖关系,创建映射文件并制作捆绑包?

返回
顶部