[AngularJS] Directive. 2 Useful directive
angularjs directive
2023-09-14 08:59:21 时间
<!DOCTYPE html> <html> <head> <title>Angular Directive</title> <link rel="stylesheet" href="foundation.min.css" /> <script src="angular.min.js"></script> <script src="main.js"></script> </head> <body ng-app="behaviorApp"> <div enter leave>I'm content!</div> </body> </html>
app.directive('enter', function(){ return function(scope, element){ element.bind("mouseenter", function(){ element.addClass("panel"); }); } }); app.directive('leave', function(){ return function(scope, element){ element.bind("mouseleave", function(){ element.removeClass("panel"); }); } });
The code is to check when you mouse enter a div, it will add a "panel" class defined in fundation.min.css, when it leaves will remove the panel calss.
We can write those code more useful!. Remove "panel" class from Javascript, add it into html.
<body ng-app="behaviorApp">
<div enter="panel" leave>I'm content!</div>
</body>
app.directive('enter', function(){ return function(scope, element, attrs){ element.bind("mouseenter", function(){ element.addClass(attrs.enter); }); } }); app.directive('leave', function(){ return function(scope, element, attrs){ element.bind("mouseleave", function(){ element.removeClass(attrs.enter); }); } });
相关文章
- AngularJS总结
- AngularJS简介
- [angularjs] angularjs系列笔记(八)事件详解编程语言
- [angularjs] angularjs系列笔记(六)http详解编程语言
- [angularjs] angularjs系列笔记(五)Service详解编程语言
- AngularJS的$watch用法详解编程语言
- AngularJS的表达式学习详解编程语言
- angularjs自定义过滤器在页面和控制器中的使用详解编程语言
- 让angularjs支持浏览器自动填表
- AngularJS入门教程之HelloWorld!
- AngularJS入门教程(零):引导程序
- AngularJS入门教程(一):静态模板
- AngularJS初始化过程分析(引导程序)
- AngularJS语法详解(续)
- AngularJS实现表单验证