angularjs实战
1.指令 transclude 保留原来的内容 replace 去掉<my-directive>指令
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
<div ng-app="a">
<my-directive><div>这是原来的<p>this is p</p></div></my-directive>
</div>
<script>
var app = angular.module('a', []);
app.directive('myDirective', function () {
return{
template:'<div>this is directive<div ng-transclude=""></div></div>',
transclude:true,
replace:true
}
});
</script>
2.指令绑定函数
<body ng-app="a">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
<div ng-controller="ctrl1">
<my-directive>鼠标移上来</my-directive>
</div>
<!--指令绑定函数 ,借助 link -->
<script>
var app = angular.module('a', []);
app.controller('ctrl1', function ($scope) {
$scope.load= function () {
console.log("loading..");
}
})
app.directive('myDirective', function () {
return{
link: function (scope,element,attrs) {
element.bind('mouseover', function () {
// scope.load();
scope.$apply("load()"); // 两种方式
})
}
}
});
</script>
3.指令复用,绑定不同函数
<body ng-app="a">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
<div ng-controller="ctrl1">
<my-directive howToLoad="load()">鼠标移上来1</my-directive>
</div>
<div ng-controller="ctrl2">
<my-directive howToLoad="load2()">鼠标移上来2</my-directive>
</div>
<!--指令复用,绑定不同函数 ,要添加不同属性 howToLoad -->
<script>
var app = angular.module('a', []);
app.controller('ctrl1', function ($scope) {
$scope.load= function () {
console.log("loading..");
}
})
app.controller('ctrl2', function ($scope) {
$scope.load2= function () {
console.log("loading.22.");
}
})
app.directive('myDirective', function () {
return{
link: function (scope,element,attrs) {
element.bind('mouseover', function () {
scope.$apply(attrs.howtoload);
})
}
}
});
</script>
4.独立指令 $scope:{} 使每个复用的hello指令不受影响,在第一个hello输入值时,第二个hello不受影响
<body ng-app="a">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
<hello></hello><br/>
<hello></hello>
<script>
var app = angular.module('a', []);
app.directive('hello', function () {
return{
template:'<input type="text" ng-model="name">{{name}}',
scope:{}
}
})
</script>
5.指令scope @
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
隔离作用域中把 myText同dom中的your-text属性绑定
<div ng-app="a">
<hello url="http://www.baidu.com" your-text="sohu"></hello>
</div>
<script>
var app=angular.module('a',[]);
app.directive('hello', function () {
return{
template:'<div><a href="{{url}}">{{myText}}</a></div>',
replace:true,
scope:{
myText:'@yourText',
url:'@'
}
}
})
</script>
angularjs实战的更多相关文章
- AngularJS实战之Controller之间的通信
我们时常会在不同controller之间进行通信,接下来就介绍三种controller之间的通信方式 一.使用$on.$emit和$broadcast进行controller通信 虽然AngularJ ...
- AngularJS实战项目(Ⅰ)--含源码
前言 钻研ABP框架的日子,遇到了很多新的知识,因为对自己而言是新知识,所以经常卡在很多地方,迟迟不能有所突破,作为一个稍有上进心的程序员,内心绝对是不服输的,也绝对是不畏困难的,心底必然有这样一股力 ...
- AngularJs学习笔记-慕课网AngularJS实战
第1章 快速上手 放弃了IE8以及以下,不支持. 4大核心特性: 1.MVC Model: 数据模型 View:视图 Controller:业务逻辑和控制逻辑 好处:职责清晰,模块化. 2.模块化 3 ...
- AngularJS 实战讲义笔记
第一部分 快速上手 1.1 感受AngularJs四大核心特性(MVC, 模块化,指令系统,双向数据绑定)1.2 搭建自动化的前端开发,调试,测试环境 代码编辑工具 (sublime) 断点调试工具 ...
- [置顶] AngularJS实战之路由ui-sref-active使用
当我们使用angularjs的路由时,时常会出现一个需求,当选中菜单时把当前菜单的样式设置为选中状态(多数就是改变颜色) 接下来就看看Angular-UI-Router里的指令ui-sref-acti ...
- 《AngularJs实战》学习笔记(慕课网)
1. Controller使用过程中的注意点 不要试图去复用Controller, 一个控制器一般只负责一小块视图 不要在Controller中操作DOM, 这不是控制器的职责. 封装在指令里. 不要 ...
- angularJS实战(一)
angular实现列表 accessCtrl.js let AccessCtrl = function($scope, AlertService, DialogService, BigDataServ ...
- AngularJS实战之cookie的读取
<!DOCTYPE html> <html ng-controller="cookies_controller"> <head> <tit ...
- AngularJS实战之ngAnimate插件实现轮播
第一步:引入angular-animate.js 第二步:注入ngAnimate var lxApp = angular.module("lxApp", [ 'ngAnimate' ...
随机推荐
- javascript 常用技巧
1. 将彻底屏蔽鼠标右键 oncontextmenu=”window.event.returnValue=false” < table border oncontextmenu=return(f ...
- 获取Spring容器Bean
WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); ManageResolver mr = (Ma ...
- ubuntu14.04 server 安装vmware worktation 12
0) Do the basic system installation of Ubuntu 14.04 LTS (Server or Desktop) 1) wget the installer wg ...
- Verify Preorder/Inorder/Postorder Sequence in Binary Search Tree
Verify Preorder Sequence in Binary Search Tree \Given an array of numbers, verify whether it is the ...
- wxpython 基本的控件 (按钮)
使用按钮工作 在wxPython 中有很多不同类型的按钮.这一节,我们将讨论文本按钮.位图按钮.开关按钮(toggle buttons )和通用(generic )按钮. 如何生成一个按钮? 在第一部 ...
- Application.AddMessageFilter(this);
开发环境:windows 8(x64), vs2013 只要“项目属性-调试”中选中“启用Visual Studio承载进程“,在VS2013中用F5调试,调用Application.AddMessa ...
- malloc原理和内存碎片
当一个进程发生缺页中断的时候,进程会陷入内核态,执行以下操作: 1.检查要访问的虚拟地址是否合法 2.查找/分配一个物理页 3.填充物理页内容(读取磁盘,或者直接置0,或者啥也不干) 4.建立映射关系 ...
- 零件分组_DP
问题 C: 零件分组 时间限制: 1 Sec 内存限制: 64 MB提交: 31 解决: 14[提交][状态][讨论版] 题目描述 某工厂生产一批棍状零件,每个零件都有一定的长度(Li)和重量(W ...
- Android 开发技巧 - Android 6.0 以上权限大坑和权限检查基类封装
简单介绍 关于运行时权限的说法,早在Google发布android 6.0的时候,大家也听得蛮多的.从用户的角度来讲,用户是受益方,更好的保护用户的意思,而对于开发者来说,无疑增加了工作量. 对于6. ...
- php生成对象的研究
<?php abstract class E{ protected $name; function __construct($name){ $this->name = $name; } a ...