angularjs学习之六(angularjs中directive指令的一般编程事件绑定 模板使用等)
angular js 中模板的使用。事件绑定以及指令与指令之间的交互
相应教学视频地址(需翻墙): v=aG8VD0KvUw4">angularjs教学视频
<!doctype html>
<html ng-app="myapp">
<head>
<meta charset="utf-8"/>
</head>
<body ng-controller="ShieldController">
<div>
<who></who>
</div>
<div>
<button you-btn></button>
</div>
<theshield reigns>343</theshield>
<theshield reigns>fdhg</theshield>
<theshield rollins>hhh</theshield>
<theshield ambros>kkk</theshield>
</body>
<script src="./js/angular.min.js"></script>
<script>
var app = angular.module('myapp',[]); /*=======================1. 模板的使用 ========================*/
app.directive('who',function(){
return {
restrict:"E", //元素element 的意思
link:function(scope,element,attrs){
console.log(element);
element[0].innerHTML = 'sdfhkj'; //这个优先级别最高
},
//templateUrl:"param.html", //这个不显示 优先级别最低
template:"<h1>jkdhf</h1>" //这个显示 优先级别其次
};
}); /*=======================2. 事件的绑定 ========================*/
app.directive('youBtn',function(){
return {
restrict:"A", //attribute 属性的意思
link:function(scope,element,attrs){
console.log(element);
element[0].innerHTML = 'my btn';
//事件绑定
element.bind('mouseenter',function(){
element[0].innerHTML = 'your btn';
});
element.bind('mouseleave',function(){
element[0].innerHTML = 'her btn';
});
}
};
}); /*=======================3. 元素 属性 控制器之间的交互========================*/ app.controller('ShieldController',function($scope){
$scope.shieldNames = [];
this.addReigns = function(){
$scope.shieldNames.push("reigns:jjj");
}
this.addRollins = function(){
$scope.shieldNames.push("Rollins:hhh");
}
this.addAmbros = function(){
$scope.shieldNames.push("Ambros:ggg");
}
})
.directive('reigns',function(){
return {
require:"theshield",
link:function(scope,element,attrs,ShieldController){
ShieldController.addReigns();
}
};
})
.directive('rollins',function(){
return {
require:"theshield",
link:function(scope,element,attrs,ShieldController){
ShieldController.addRollins();
}
};
})
.directive('ambros',function(){
return {
require:"theshield",
link:function(scope,element,attrs,ShieldController){
ShieldController.addAmbros();
}
};
})
.directive('theshield',function(){
return {
restrict:"E",
controller:"ShieldController", //指定控制器
scope:{}, //清空该指令处的$scope 值
link:function(scope,element,attrs){
element.bind('mouseenter',function(){ //对于该指令所相应的元素绑定相应的事件
console.log(scope.shieldNames);
});
}
};
}); </script>
</html>
angularjs学习之六(angularjs中directive指令的一般编程事件绑定 模板使用等)的更多相关文章
- Angularjs学习---ubuntu12.04中karma安装配置
Angularjs学习---ubuntu12.04中karma安装配置中常见的问题总结 karma启动时出现了很多问题: 1.安装karma前提条件 安装karma首先要安装nodejs,npm然 ...
- MVVM设计模式和WPF中的实现(四)事件绑定
MVVM设计模式和在WPF中的实现(四) 事件绑定 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中 ...
- MVVM设计模式和在WPF中的实现(四) 事件绑定
系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中的实现(三)命令绑定 MVVM模式解析和在WPF中的 ...
- AngularJS中Directive指令系列 - 基本用法
参考: https://docs.angularjs.org/api/ng/service/$compile http://www.zouyesheng.com/angular.html Direct ...
- angularjs中directive指令与component组件有什么区别?
壹 ❀ 引 我在前面花了两篇博客分别系统化介绍了angularjs中的directive指令与component组件,当然directive也能实现组件这点毋庸置疑.在了解完两者后,即便我们知道co ...
- 《AngularJS权威教程》中关于指令双向数据绑定的理解
在<AngularJS权威教程>中,自定义指令和DOM双向数据绑定有一个在线demo,网址:http://jsbin.com/IteNita/1/edit?html,js,output,具 ...
- angularjs学习笔记3-directive中scope的绑定修饰符
在angularjs中,一个directive返回一个对象,对象存在很多属性,并且可以在directive中自定义自己的scope,而使用自己的scope是为了防止一个directive被使用在多个地 ...
- AngularJs学习——何时应该使用Directive、Controller、Service?
翻译:大漠穷秋 原文链接:http://kirkbushell.me/when-to-use-directives-controllers-or-services-in-angular/ 一.简述 A ...
- angularjs学习总结一(表达式、指令、模型)
一:自执行匿名函数 (function(){ /*code*/})();自执行匿名函数:常见格式:(function() { /* code */ })();解释:包围函数(function(){}) ...
随机推荐
- 华为S5700交换机初始化和配置SSH和TELNET远程登录方法
基础设置: 配置登陆IP地址<Quidway> system-view ...
- Go:channel
一.channel 在 Go 语言里,不仅可以使用原子函数和互斥锁来保证对共享资源的安全访问以及消除竞争状态,还可以使用 channel,通过发送和接收需要共享的资源,在 goroutine 之间做同 ...
- 剑指Offer(书):二进制中1的个数
题目:输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 分析:下面这两种方法都可以,不过第二种更好一些. public int numberOf1(int n) { int count ...
- 【HDU 6000】Wash(贪心)
Problem Description Mr.Panda is about to engage in his favourite activity doing laundry! He's brough ...
- C#中对泛型List进行分组输出元素
背景:在输出列表时,往往需要按照某一字段进行分组,比如在输出城市列表时,按照首字母进行分组,输出学生列表时,按照年级进行分组,然后再对分组的结果按照其他的字段进行排序. 如存在以下STU学生类,代码如 ...
- 了解DOM
DOM是为了方便处理层次型文档(如XML.HTML)的一种技术.DOM还提供了一套API,使开发人员可以用面向对象的方式来处理这些文档.对于XML文档来说,有专门的处理XML文档是XML DOM,一 ...
- 用spring annotation声明的bean,当打包在jar中时,无法被扫描到
发现一个问题,十分蛋疼. 我们项目是由N个工程组成的,外围工程是web工程,内部的工程打包成jar,放入外围工程的WEB-INF/lib 内部的工程用到了spring的注解,例如@Service.@C ...
- html template & import link bug
html template & import link bug html templates is OK https://caniuse.com/#search=html%20template ...
- HDU 3911 区间合并求最大长度的问题
http://vjudge.net/problem/viewProblem.action?id=21557 题目大意: 每进行一次颜色改变都可以把一段区间内的黑石头变成白石头,白石头变成黑石头,最后问 ...
- spoj 839 最小割+二进制
#include<stdio.h> #include<string.h> #include<queue> using namespace std; #define ...