自定义指令

1、第一个参数是指令的名字,第二个参数任然应该使用一个数组,数组的最后一个元素是一个函数。定义指令的名字,应该使用驼峰命名法

 <!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css">
</head> <body ng-app="demoApp">
<!-- <newsButton></newsButton> -->
<!-- <news-button></news-button> -->
<!-- <div newsButton></div> -->
<btn-primary></btn-primary>
<btn-danger></btn-danger>
<script src="bower_components/angular/angular.js"></script>
<script>
var demoApp = angular.module('demoApp', []); // 第一个参数是指令的名字,第二个参数任然应该使用一个数组,数组的最后一个元素是一个函数
// 定义指令的名字,应该使用驼峰命名法
demoApp.directive('newsButton', [function() {
// 该函数应该返回一个指令对象
return {
template:'<input type="button" value="news" class="btn btn-lg btn-primary btn-block" />'
};
}]); // demoApp.directive('btnPrimary', [function() {
// return {
// template:'<input type="button" value="news" class="btn btn-primary" />'
// };
// }]); // demoApp.directive('btnDanger', [function() {
// return {
// template:'<input type="button" value="news" class="btn btn-danger" />'
// };
// }]); // demoApp.directive('btnSuccess', [function() {
// return {
// template:'<input type="button" value="news" class="btn btn-success" />'
// };
// }]); demoApp.controller('DemoController', ['$scope', function($scope) {
// $scope.xxxx=xxx;
// $scope.do=function() { // };
// $scope.$watch('',function(now,old) { // });
}]);
</script>
</body> </html>

2、自定义一个面包屑导航

 <!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css">
</head> <body ng-app="demoApp">
<!-- <btn>itcast</btn> -->
<div breadcrumb></div>
<breadcrumb data=""></breadcrumb>
<script src="bower_components/angular/angular.js"></script>
<script>
var demoApp = angular.module('demoApp', []); demoApp.directive('breadcrumb', [function() {
// Runs during compile
return {
// 指定当前指令的类型什么样的
// restrict: 'EA',
// // E = Element, A = Attribute, C = Class, M = Comment
// template: '', // 模版字符串
templateUrl: 'tmpls/breadcrumb.html',
replace: true,
// transclude: true,
};
}]); // demoApp.directive('btn', [function() {
// return{
// scope:{
// primary:'@',
// lg:'@',
// block:'@',
// },
// template:'<button class="btn {{primary==\'true\'?\'btn-primary\':\'\'}}">button</button>'
// }
// }]); // demoApp.directive('btn', [function() {
// return {
// // 指令对象的transclude必须设置为true才可以在模版中使用ng-transclude指令
// transclude: true,
// replace: true, // 替换指令在HTML中绑定的元素
// template: '<button class="btn btn-primary btn-lg" ng-transclude></button>'
// };
// }]);
</script>
</body> </html>

3、面包屑导航

 <!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<title>封装一个面包屑导航</title>
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css">
</head> <body ng-app="myApp" ng-controller="DemoController">
<breadcrumb data="{{pathData1}}"></breadcrumb>
<breadcrumb data="{{pathData2}}"></breadcrumb>
<script src="bower_components/angular/angular.js"></script>
<script>
var myApp = angular.module('myApp', []); myApp.controller('DemoController', ['$scope', function($scope) {
$scope.pathData1 = {
home: '#',
news: '#',
itheima: '#',
bbs: '#'
};
$scope.pathData2 = {
home: '#',
library: '#',
data: '#'
};
}]); // 定义一个面包屑导航指令
myApp.directive('breadcrumb', [function() {
// 返回指令对象
return {
scope: {},
templateUrl: 'tmpls/breadcrumb.html',
replace: true,
link: function(scope, element, attributes) {
scope.data = JSON.parse(attributes.data);
// console.log(scope.data);
}
};
}]);
</script>
</body> </html>

第216天:Angular---自定义指令(二)的更多相关文章

  1. Angular自定义指令(directive)

    angular自定义指令,意我们可以通过angula自己定义指令,来实现我们的特殊要求,为所欲为,一支穿云箭,千军万马来相见 多少年的老规矩了,先看代码: <!DOCTYPE html> ...

  2. angular 自定义指令详解 Directive

    在angular中,Directive,自定义指令的学习,可以更好的理解angular指令的原理,当angular的指令不能满足你的需求的时候,嘿嘿,你就可以来看看这篇文章,自定义自己的指令,可以满足 ...

  3. Angular自定义指令directive:scope属性

    在AngularJS中,除了内置指令如ng-click等,我们还可以自定义指令.自定义指令,是为了扩展DOM元素的功能.代码中,通过指定directive中的restrict属性,来决定这个指令是作为 ...

  4. angular 自定义指令 directive transclude 理解

    项目中断断续续的用了下angular,也没狠下心 认真的学习.angular 特别是自定义指令这块 空白. transclude 定义是否将当前元素的内容转移到模板中.看解释有点抽象. 看解释有点抽象 ...

  5. Angular17 Angular自定义指令

    1 什么是HTML HTML文档就是一个纯文本文件,该文件包含了HTML元素.CSS样式以及JavaScript代码:HTML元素是由标签呈现,浏览器会为每个标签创建带有属性的DOM对象,浏览器通过渲 ...

  6. angular自定义指令

    1.在directive文件下创建指令的js文件 通常自定义指令需要声明模块(注意定义指令时, js内部指令名称需采用 aaAaBb驼峰的命名方式  html中使用的是aa-aa-bb) e.g (f ...

  7. angular -- 自定义指令和模板

    angular 可以自定义一些指令,来简化我们前端的工作量. 第一种:简单指令示例: <h3>自定义指令</h3> <sheng></sheng> &l ...

  8. angular自定义指令命名的那个坑

    Directive 先从定义一个简单的指令开始. 定义一个指令本质上是在HTML中通过元素.属性.类或注释来添加功能.AngularJS的内置指令都是以ng开头,如果想自定义指令,建议自定义一个前缀代 ...

  9. angular自定义指令相关知识及代码

    原文地址 https://www.jianshu.com/p/0c015862156d 大纲 1.自定义指令之——属性指令 2.自定义属性指令的运行原理 3.自定义属性指令代码实践 4.自定义结构指令 ...

  10. angular自定义指令 repeat 循环结束事件;limitTo限制循环长度、限定开始位置

    1.获取repeat循环结束: 自定义指令: .directive('repeatFinish', function () { return { link: function (scope, elem ...

随机推荐

  1. html元素可以同时实现两个class的功能

    html元素标签可以放置两个不同的class文件并分别实现效果

  2. django套用模板404报错小结

    首先,我的项目名是MyProject.每次当我运行,然后测试页面的时候,总是弹出 其实根据stackoverflow上某大佬的解释大意就是在setting.py和urls.py的匹配上出了问题 此处放 ...

  3. 如何配置pycaffe

    首先,使用cmake配置.生成caffe的vs2015工程时,设定生成python接口,即BUILD项->BUILD_python.BUILD_python_layer,注意使用CMake生成V ...

  4. 为什么使用React Native

    React Native使你能够在Javascript和React的基础上获得完全一致的开发体验,构建世界一流的原生APP. React Native着力于提高多平台开发的开发效率 —— 仅需学习一次 ...

  5. PCL 库存在vtk的问题导致libproj.so链接错误

    常变现为** No rule to make target '/usr/lib/x86_64-linux-gnu/libproj.so', needed by ××× vtk库的bug导致,目前尚未修 ...

  6. 01-numpy基础简介

    import numpy as np # ndarray ''' # 三种创建方式 1.从python的基础数据对象转化 2.通过numpy内置的函数生成 3.从硬盘(文件)读取数据 ''' # 创建 ...

  7. word2vec的理解

    在学习LSTM的时候,了解了word2vec,简单的理解就是把词变成向量.看了很多书,也搜索了很多博客,大多数都是在word2vec的实现原理.数学公式,和一堆怎么样重新写一个word2vec的pyt ...

  8. 关于go语言中的WaitGroup

    如果你刚接触Go语言并且想用它构建高并发,高性能的应用,弄明白WaitGroups是怎么回事很重要. 在本教程中,我们将掌握以下内容: WaitGroups的用途 一个WaitGroups的简单示例 ...

  9. Django_csrf

    CSRF攻击介绍 CSRF 攻击可以在受害者毫不知情的情况下以受害者名义伪造请求发送给受攻击站点,从而在并未授权的情况下执行在权限保护之下的操作.比如说,受害者 Bob 在银行有一笔存款,通过对银行的 ...

  10. 王者荣耀交流协会final发布第四次scrum例会

    1.例会照片 成员高远博,冉华,王磊,王玉玲,任思佳,袁玥,王磊,王超同学因参加比赛不在学校,不能出席. master:王玉玲 2.时间跨度 2017年12月4日 18:00 — 18:18,总计18 ...