当前位置: 首页 > news >正文

AngularJS中有关Directive的汇总

 

本篇通过几个例子对AngularJS中的Directive进行汇总。

例子1,单向绑定和双向绑定

 

<html ng-app="myApp">
  <head>
    <script src="angualr.js"></script>
    <script>
        (function(){
            var name = "myApp";
            requried = [];
            myApp = null;
            
            myApp = angualr.module(name, requires);
            
            myApp.controller("AppCtrl", functioin($scope){
                $scope.contacts = [
                    {firstname: "", lastname: "'},
                    ...
                ];
                
                $scope.addContact = function(){
                    $scope.contacts.push({firstname:"", lastname:"", isEnabled:true});
                }
                
                //切换视图
                $scope.viewFile = function(){
                     if($scope.viewState){
                        return "contact_list.html";
                     } else{
                        return "contact_table.html";
                     }
                }
                
                $scope.onPartialLoad = function(){
                    console.log($scope.viewFile() + " loaded");
                }
            })
        }());
    </script>
  </head>
  <body ng-controller="AppCtrl">
    
  </body>
</html>

 

==单向绑定

 

{{contacts.length}}
<div ng-bind="contacts.length"></div>
<div ng-bind-template="">the first name is {{contacts[0].firstname}}</div>
{{contacts[0].firstname}}
{{::contacts.length}} 只展示一次数组长度,当数组长度有改变,这里不变
{{ 2 + 3 }}
{{ Math.min(4, 2)}}
<button ng-click="addContact()">添加</button>
<div ng-non-bindable>this is {{hello }}</div> 这里的{{hello}}会显示出来

...
<tr ng-repeat="contact in contacts" ng-class="$odd ? 'odd':'even'">
    <td>{{$index + 1}}</td>
    <td>{{contact.firtname}}</td>
    <td>{{contact.lastname}}</td>
    <td>{{contact.isEnabled}}</td>
    <td>{{$first}}</td>
    <td>{{$last}}</td>
    <td>{{$middle}}</td>
</tr>
...

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


//切换视图
<input type="checkbox" ng-model="viewState">切换视图
<ng-include src="viewFile()" onload="onPartialLoad()"></ng-include>

 

==使用Directive的几种方式

<div ng-bind="contacts.length"></div>
<div class="ng-bind:contacts.length"></div>
<ng-include></ng-include>

==双向绑定

<input type="text" ng-model="contacts[0].firstname"/>

例子2,ng-switch

 

<html ng-app="myApp">
    <head>
        angular.js
        <script>    
            (function(){
                var name = "myApp[]",
                requires = [],
                myApp = null;
                
                myApp = angular.module(name, requires);
                myApp.controller("AppCtrl", function($scope){
                    $scope.data = {};
                });
            }());
        </script>
    </head>
    <body ng-controller="AppCtrl">
    </body>
</html>

 

页面部分

 

<div ng-repeat="channel in ['None', 'Tv', 'kitty']"  ng-cloak>
    <input type="radio" name ="leisure" value="{{channel}}" ng-model="data.whichChannel" ng-checked="$first" />{{channel}}
</div>

<div ng-switch on="data.whichChannel">
    <div ng-switch-default>this is none</div>
    <div ng-switch-when="Tv">this is tv</div>
    <div ng-switch-when="kitty">this is kitty</div>
</div>

 

以上,

● ng-checked 勾选
● ng-switch切换显示其中的内容
● 当点击Tv相关的这个RadioButton,把Tv这个值赋值给了data对象的whichChannel字段,whichChannel字段值得改变会告诉ng-swich所在的div,其子元素的ng-switch-when值如果和当前的whichChannel字段值匹配,就显示
● ng-cloak 避免绑定数据的时候页面闪烁

例子3,显示、隐藏、移除元素,ng-show, ng-hide, ng-if

$scope.toggleNewContact = false;
$scope.shwoNewContactForm = function(){
    $scope.toggleNewContact = true;
}


<button ng-click="showNewContactForm()">Add New Contact</button>
<form ng-show="toggleNewContact">
    <button ng-click="toggleNewContact = false">Cancel</button>
</form>


<tr ng-repeat="contact in contacts" ng-if="contact.isEnabled">
</tr>

 

例子4,勾选,只读,禁用,链接

 

$scope.checkMe = true;
$scope.url = "http://google.com";
$scope.imgSrc = "hi.jpeg";

//勾选
<input type="checkbox" ng-checked="{{checkME}}" /> check me

//禁用按钮
<button ng-disabled="{{checkMe}}">Click me</button>

//只读
<input type="text" value="he" ng-readonly="{{checkMe}}" />

//链接
<a href="{{url}}">go</a>
<a ng-href="{{url}}">go</a> 推荐使用

//图片
<img ng-src="{{imgSrc}}"/>

 

例子5,ng-style

 

<button ng-click="styles={'color':'red'}">set color</button>
<button ng-click="styles={'font-weight':'bold'}">bold</button>
<button ng-click="styles={'font-style':'italic'}>italic></button>
<p ng-style="styles">hello</p>

 

例子6,ng-class

 

.strike{
    text-decoration:line-through;
}

.bold{
    font-weight:bold;
}

.red{
    color:red;
}

==把一个值赋值给ng-class

//文本框和controller中的style变量绑定起来
<input type="text" ng-model="style" />
<p ng-class="style">he</p>

==把一个对象赋值给ng-class

<input type="checkbox" ng-model="deleted" /> deleted
<input tyep="checkbox" ng-model="important" /> important
<input type="checkbox" ng-model="error"> error
<p ng-class="{strike:deleted, bold:important, red:error}">hello</p>


==把一个数组赋值给ng-class

//运用所有的class
<p ng-class="['strike','bold','red']">hi</p>

另外,

<tr ng-repeat="contact in contacts" ng-class-odd="'odd'" ng-class-even="'even'"></tr>

例子7, 事件

ng-click, ng-mousedown, ng-mouseenter, ng-mouseleave, ng-mouseup

例子8,过滤


==对数组元素过滤

 

$scope.courses = [
    {name:"", category:"", timeline:20, price:25},
    ...
];

$scope.getTargetDate = function(days){
    var now = new Date();
    return now.setDate(now.getDate() + days);
}

<tr ng-repeat="course in courses">
    <td>{{$index + 1}}</td>
    <td>{{course.name | upplercase}}</td>
    <td>{{course.category | lowercase }}</td>
    <td>{{getTargetDate(course.timeline) | date: 'dd MMM yy' | uppercase }}</td>
    <td>{{course.price | currency: "¥" }}</td>
    <td>{{course | json}}</td>
</tr>

 

==对整个数组过滤

 

$scope.limitVal = 10;
$scope.lessThan25 = function(item){
    return item.price < 25;
}

{{courses.length}}
<button ng-click="limitVal = 5">5</button>
<button ng-click="limitVl = 10">10</button>
//<input type="text" ng-model="searchStr" />
//<input type="text" ng-model="name" />
//<input type="text" ng-model="category"/>

//<tr ng-repeat = "course in courses | limitTo: limitVal | filter: searchStr">
//<tr ng-repeat = "course in courses | limitTo: limitVal | filter: {name: name, category:category}">
//<tr ng-repeat = "course in courses | limitTo: limitVal | filter: lessThan25">
//<tr ng-repeat = "course in courses | limitTo: limitVal | orderBy: '-price'">
<tr ng-repeat = "course in courses | limitTo: limitVal | orderBy: ['name','-price']">
    <td>{{$index + 1}}</td>
    <td>{{course.name}}</td>
    <td>{{course.category}}</td>
    <td>{{course.timeline}}</td>
    <td>{{course.price}}</td>
</tr>


所以filter能接受的包括字符串、对象和函数。

 

相关文章:

  • java架构面试锦集:开源框架+并发+数据结构+大企必备面试题
  • 复合格式化 AppendFormat 字符
  • BZOJ 2337 XOR和路径(高斯消元)
  • Adas术语简称
  • extern c 谈
  • 转载 一堂价值39万元的课,把她看完,你一定会有所获!
  • 关于centos联网的问题
  • 第二章 Java内存区域与内存溢出异常
  • java 用进程调用外部命令并获取返回结果
  • 购物狂欢节的背后,是谁在让你在吃土?
  • Hadoop之HDFS中NameNode的工作机制
  • 目录操作常用命令
  • 史上最全的Python开发秘籍,学完这些年薪百万不是问题
  • 设计模式编写表单验证
  • spark hive python依赖第三方包
  • 【知识碎片】第三方登录弹窗效果
  • 08.Android之View事件问题
  •  D - 粉碎叛乱F - 其他起义
  • Debian下无root权限使用Python访问Oracle
  • express如何解决request entity too large问题
  • extjs4学习之配置
  • Java应用性能调优
  • js ES6 求数组的交集,并集,还有差集
  • node和express搭建代理服务器(源码)
  • scrapy学习之路4(itemloder的使用)
  • 更好理解的面向对象的Javascript 1 —— 动态类型和多态
  • 3月27日云栖精选夜读 | 从 “城市大脑”实践,瞭望未来城市源起 ...
  • PostgreSQL之连接数修改
  • 回归生活:清理微信公众号
  • ​LeetCode解法汇总2583. 二叉树中的第 K 大层和
  • #define,static,const,三种常量的区别
  • #pragma multi_compile #pragma shader_feature
  • ()、[]、{}、(())、[[]]命令替换
  • (1)虚拟机的安装与使用,linux系统安装
  • (16)Reactor的测试——响应式Spring的道法术器
  • (2022版)一套教程搞定k8s安装到实战 | RBAC
  • (C#)Windows Shell 外壳编程系列9 - QueryInfo 扩展提示
  • (分类)KNN算法- 参数调优
  • (利用IDEA+Maven)定制属于自己的jar包
  • (入门自用)--C++--抽象类--多态原理--虚表--1020
  • (详细版)Vary: Scaling up the Vision Vocabulary for Large Vision-Language Models
  • (学习日记)2024.04.10:UCOSIII第三十八节:事件实验
  • ./mysql.server: 没有那个文件或目录_Linux下安装MySQL出现“ls: /var/lib/mysql/*.pid: 没有那个文件或目录”...
  • .[hudsonL@cock.li].mkp勒索加密数据库完美恢复---惜分飞
  • .md即markdown文件的基本常用编写语法
  • .MSSQLSERVER 导入导出 命令集--堪称经典,值得借鉴!
  • .net程序集学习心得
  • .net通用权限框架B/S (三)--MODEL层(2)
  • [ 常用工具篇 ] POC-bomber 漏洞检测工具安装及使用详解
  • [100天算法】-实现 strStr()(day 52)
  • [C++]命名空间等——喵喵要吃C嘎嘎
  • [Deep Learning] 神经网络基础
  • [Editor]Unity Editor类常用方法
  • [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c
  • [ERROR] ocp-server-ce-py_script_start_check-4.2.1 RuntimeError: ‘tenant_name‘