angularjs的一些使用经验总结,此篇文章单谈ng指令之一ngrepeat

1. ngrepeat 时报错 Duplicates in a repeater are not allowed,

正常的时候,我们表达式内的数据是类似这样的(这时不会有任何问题)

<span ng-repeat="answer in ['good answer','perfect answer', 'bad answer','not bad answer'] ">{{answer}} </span>

但一旦当我们表达式的数据取到的是类似这样的

<span ng-repeat="answer in ['aa','bb','cc','cc','bb'] track by $index">{{answer}} </span>

便会报错 Duplicates in a repeater are not allowed,
    此时他会用英文提示你 use track by 表达式
    发生此错误的原因是 ngrepeat 的 数组或者对象数组或者对象 的每个迭代项中有重复值
    因为当使用ngrepeat不加track by表达式时,默认的为 item in items is equivalent to item in items track by $id(item)
    所以出现重复值时就会报错 页面repeat的那个dom元素不会在视图里显示出来

2.ngrepeat的filter

ng-repeat指令有一个特有的filter 名为filter的filter 用来对数据内容进行具体的过滤

同时当然也可以在使用angularjs框架的一些预先内置filter,如limitto (设置只显示一个)

ng-repeat="item in items | orderBy:'oreder_prop' | filter:query | limitTo:4"  (类似这样的语法使用)

ng-repeat="course in home.my_join_course | limitTo:1"

对这个特别的名为filter的使用

<div class="box">
<div class="col-xs-12">
<div ng-init="output = [{name:'a',addr:'东京',score:22},{name:'b',addr:'西京',score:10},{name:'c',addr:'南京'},{name:'d',addr:'北京',area:'china'}]" >

        <p ng-repeat="print in output | filter: 'china' ">{{print}}</p> <!-- 只显示属性值里有china的 -->
<p ng-repeat="print in output | filter: {name:'b'} ">{{print}}</p> <!-- 只显示name属性值里有b的 --> <p ng-repeat="print in output | filter: have_score_than_15 ">{{print}}</p>
<!-- 只显示score属性大于15的 即只显示name:a,addr:东京,score:22的那项 -->
</div>
</div>
</div> $scope.have_score_than_15 = function(item) {
return item.score > 15;
}

另外再简单介绍下排序 (这点直接引用:http://www.geekcome.com/content-10-1906-1.html )如下

<div>{{ childrenArray | orderBy : 'age' }}</div>      //按age属性值进行排序,若是-age,则倒序
<div>{{ childrenArray | orderBy : orderFunc }}</div> //按照函数的返回值进行排序
<div>{{ childrenArray | orderBy : ['age','name'] }}</div> //如果age相同,按照name进行排序

此外还可以支持自定义过滤器满足一些复杂的自定义操作如

<div ng-repeat=" depart in departments | child_department "> .... </div>

appFilters.filter('child_department',function () {
return function (departments) {
var departmentId = 3;
var array = [];
$.each(departments,function(i,n){
if(n.parent_id == departmentId){
array.push(n);
}
});
return array;
}
});

3.活用 $index     $first     $middle     $last     $even     $odd

     <ul class="dropdown-menu pull-right dropdown-menu-right" >
<li app-pressdark ng-click="course_filter('','depart','全部科室')" class="dropdown-divider">全部科室</li>
<li app-pressdark ng-repeat="depart in departs | filter:{parent_id:null}" ng-click="course_filter(depart.id,'depart',depart.name)" class="{{$last?'dropdown-nodivider':'dropdown-divider'}} ">
{{depart.name}}
</li>
</ul>

在 class="{{$last?'dropdown-nodivider':'dropdown-divider'}} " 上利用三元表达式对repeat的项进行个别的样式处理

$index number iterator offset of the repeated element (0..length-1)
$first boolean true if the repeated element is first in the iterator.
$middle boolean true if the repeated element is between the first and last in the iterator.
$last boolean true if the repeated element is last in the iterator.
$even boolean true if the iterator position $index is even (otherwise false).
$odd

ngrepeat 时注意的地方和一些little tricks的更多相关文章

  1. 在angular中使用ng-repeat时数组中有重复元素,要用item in items track by $index

    在angular中使用ng-repeat时数组中有重复元素,要用item in items track by $index,不然会报错 <div class="" ng-in ...

  2. Java 中long类型转换成为int类型时可能会出错的地方

    那计算两个日期之间间隔的天数为例来说明这个问题. 下面是计算日期间隔天数的简单算法(主要出错的地方为红色标注的地方): public int getDay(String startDate, Stri ...

  3. angular 当使用ng-repeat 时出现 $$hashKey的键值对

    小问题 把: ng-repeat="item in items " 改成 : ng-repeat="item in items track by $index"

  4. Properties没有被注意的地方

    源起: 今天阅读源码时发现一个地方不理解: 为什么以下代码第10行 get() 之后value为null时还去 getProperty() 呢? org.springframework.util.Co ...

  5. oracle 使用count()函数进行分组计数时所踩的坑!

      1.情景展示 需要对id_card字段按字符长度进行分组统计并进行计数. 2.错误方式 第一步:统计出id_card字段共存在几种情况. 第一种方式:distinct 第二种方式:group by ...

  6. 面试题HTML +CSS

    HTML+CSS部分1.行内元素和块级元素?img算什么?行内元素怎么转化为块级元素?行内元素:和有他元素都在一行上,高度.行高及外边距和内边距都不可改变,文字图片的宽度不可改变,只能容纳文本或者其他 ...

  7. AngularJS 开发中常犯的10个错误

    简介 AngularJS是目前最为活跃的Javascript框架之一,AngularJS的目标之一是简化开发过程,这使得AngularJS非常善于构建小型app原型,但AngularJS对于全功能的客 ...

  8. AngularJS开发人员最常犯的10个错误

    简介AngularJS是目前最为活跃的Javascript框架之一,AngularJS的目标之一是简化开发过程,这使得AngularJS非常善于构建小型app原型,但AngularJS对于全功能的客户 ...

  9. AngularJS开发最常犯的10个错误

    简介 AngularJS是目前最为活跃的Javascript框架之一,AngularJS的目标之一是简化开发过程,这使得AngularJS非常善于构建小型app原型,但AngularJS对于全功能的客 ...

随机推荐

  1. asp.net高并发网站解决方案【未完成版本】

    场景:假设现在是一个电商网站,今天要举办活动,有10个商品低价销售,但是会来抢购的人会特别多,最后只有十个人可以成功的买到商品   明确2个问题 1.访问量:抢票时间断用户访问量 2.并发:1秒内请求 ...

  2. A Country on Wheels【车轮上的国家】

    A Country on Wheels As cultural symbols go, the American  car is quite young. 作为文化象征的美国汽车还相当年轻. The ...

  3. 20145202马超 《Java程序设计》第九周学习总结

    JDBC 数据库本身是个独立运行的应用程序 撰写应用程序是利用通信协议对数据库进行指令交换,以进行数据的增删查找. JDBC(Java DataBase Connectivity)是Java联机数据库 ...

  4. 8,实例化Flask的参数 及 对app的配置

    Flask 是一个非常灵活且短小精干的web框架 , 那么灵活性从什么地方体现呢? 有一个神奇的东西叫 Flask配置 , 这个东西怎么用呢? 它能给我们带来怎么样的方便呢? 首先展示一下: from ...

  5. 【vim环境配置】在centos6.4上配置vim的一些零碎记录

    上一篇日志已经step by step地实录了如何在本机mac上配置vim开发环境已经各种插件. 有了一定经验之后,开始在实验室远程server上centos6.4的环境下配置vim环境. 这台机器是 ...

  6. leetcode 【 Remove Duplicates from Sorted List II 】 python 实现

    题目: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct  ...

  7. leetcode 【 Remove Nth Node From End of List 】 python 实现

    题目: Given a linked list, remove the nth node from the end of list and return its head. For example, ...

  8. 程序员必备PC维修法(软件篇)

    学会使用专业软件检测与修复电脑硬件故障问题也是程序员的一种软技能. windows篇 情景:如何获取电脑硬件的真实信息.(如何检验选购回来的硬件是否正品) 自检:使用AIDA64软件检查电脑硬件,能详 ...

  9. 恢复误删除表黑科技之relay log大法(续)

      Preface       I've stuck twice in my previous experiments in backing up dropped tables.I am still ...

  10. 线段树(单点更新,区间查询) HDU 1754 I Hate It

    题目链接 线段树的模板 #include<iostream> #include<cstdio> #include<cmath> #include<algori ...