Angular 学习笔记 3
来源:互联网 发布:js获取html data属性 编辑:程序博客网 时间:2024/06/10 03:23
诸如 ng-controller ,ng-repeat,它们都被称之为 directive。
第一个 directive,helloworld。
angular.module('myApp', []) .directive('helloworld', function () { return { restrict: 'E', replace: true, template: '<div>Hi, this is my first directive.</div>' } });
<body ng-app="myApp"><helloworld></helloworld></body>
第二个 directive, hellowhat。
angular.module('myApp', []) .directive('hellowhat', function () { return { restrict: 'E', replace: true, template: '<div>Hi, {{what}}.</div>', scope: { what: '@' } } });
<body ng-app="myApp"><hellowhat what="Linda"></hellowhat></body>
这里 directive 使用了独立的 scope,而不是访问外部的 scope。
@ 表示 what 的值有 directive 的 what 属性传递进来。
实战一下,alert Demo。
http://v3.bootcss.com/components/#alerts。
然后
angular.module('myApp', []) .directive('alert', function () { return { restrict: 'E', replace: true, template: '<div class="alert alert-{{type}}">{{value}}</div>', scope: { type: '@', value: '@' } } }) .controller('myCtrl', function ($scope) { $scope.classes = [ "success", "info", "danger", "warning" ] }) ;
<body ng-app="myApp"><head> <link href="bootstrap/dist/css/bootstrap.css" rel="stylesheet"></head><div ng-controller="myCtrl"> <alert ng-repeat="class in classes" type="{{class}}" value="{{class}}"> </alert></div></body>
好吧,已经很酷了,但是还是有些问题。
第一,从习惯上讲,
<alert>value</alert>的写法更符合人们的习惯。
第二,bootstrap 使用 jquery 能够实现可关闭的警告框。
那么
angular.module('myApp', []) .directive('alert', function () { return { restrict: 'E', replace: true, transclude: true, template: '<div ng-hide="isClosed()" class="alert alert-{{type}}">' + '<button class="close" ng-click="close()"><span aria-hidden="true">×</span></button>' + '<div ng-transclude></div>' + '</div>', scope: { type: '@' }, controller: function ($scope) { $scope.closed = false; $scope.isClosed = function () { return $scope.closed; }; $scope.close = function () { $scope.closed = true; } } } }) .controller('myCtrl', function ($scope) { $scope.classes = [ "success", "info", "danger", "warning" ] }) ;
<body ng-app="myApp"><head> <link href="bootstrap/dist/css/bootstrap.css" rel="stylesheet"></head><div ng-controller="myCtrl"> <alert ng-repeat="class in classes" type="{{class}}" value="{{class}}"> </alert></div></body>
第一个问题,是通过 transclude 来实现的。
transclude 表示将 directive 内部的节点,替换到模板中 ng-transclude 标记的节点内。
第二个问题,则是通过 ng-hide ng-click 等内置 directive 实现的。
0 0
- Angular 学习笔记 3
- Angular JS 学习笔记3
- Angular学习笔记(3)
- angular学习笔记二
- Angular 学习笔记 1
- Angular 学习笔记 2
- Angular 学习笔记 4
- Angular 学习笔记 5
- Angular 学习笔记 6
- Angular.js学习笔记
- angular学习笔记一
- [angular]学习笔记
- angular js学习笔记
- Angular 学习笔记
- Angular学习笔记
- angular学习笔记
- Angular.js 学习笔记
- Angular Material学习笔记
- POJ3422简单费用流
- 躺坑了,self.navigationController.navigationBar setTranslucent:YES
- HashMap和Hashtable的区别
- 补第八周项目4--个人所得税计算器--if else
- 对于罗永浩个人的看法
- Angular 学习笔记 3
- UVA10780幂和阶乘
- 黑马程序员--UI之UIview、UITextField、NSUserDefaults以及键盘的升起与回落的基本操作
- Google技术开发指南:给大学生自学的建议
- 程序员,我们应该如何去学习
- UVA10943简单递推
- Largest Rectangle in Histogram
- 程序员学英语——In November the English learning summary .
- UVA11388GCD LCM