首页 技术 正文
技术 2022年11月15日
0 收藏 691 点赞 4,783 浏览 1617 个字

今天我终于鼓起勇气写自己的博客了,激动与害怕并存,希望大家能多多批评指导,如果能够帮助大家,也希望大家点个赞!!

用angularjs 工作也有段时间了,总体感觉最有挑战性的还是指令,因为没有指令的angularjs 就是只有骨头的框架,虽然有很多第三方指令,如:angular Bootstrap,ng-table等,但是根据界面设计的需求,他们远远不能满足,怎么办??答案只有自己写了(也可以google,但是为了某个小功能,引入一个很大的文件,我是不提倡的。如果老板想让你时不时的改改,我估计你会崩溃的,是不是有想辞职的想法,为了让工作有意义和提高自己的水平,还是在时间充足的情况下自己写吧!),那么现在让我们开始吧!

今天先开始一个入门级的指令:按钮点击,加入loading,阻止再次点击(这在提交表单,ajax请求数据时非常有用);

      自己小试牛刀,写了一个(虽然google 很多)。

      

 <!DOCTYPE html>
<html ng-app="myApp">
<head lang="en">
<meta charset="UTF-8">
<title></title>
<link href="../../bower_components/bootstrap/dist/css/bootstrap.min.css" rel="external nofollow" rel="stylesheet">
<script src="../../bower_components/angular/angular.js"></script>
</head>
<body ng-controller="myCtrl">
<button class="btn btn-primary" btn-loading-text="loading" trigger-loading="beginLoading" ng-click="toggleLoad()">load</button>
<button class="btn btn-default" ng-click="toggleLoad()">切换按钮状态</button>
</body>
<script>
angular.module('myDirectives',[])
.directive('triggerLoading',function(){
return {
restrict:'A',
link:function(scope,element,attr){
scope.prevText=element.text();
scope.$watch(function(){
return scope.$eval(attr.triggerLoading);
},function(value){
if(angular.isDefined(value)){
//element.toggleClass('disabled',value);
value?element.attr('disabled',true):element.removeAttr('disabled');
element.text((value?attr.btnLoadingText:scope.prevText));
}
});
}
}
});
angular.module('myApp',['myDirectives'])
.controller('myCtrl',['$scope',function($scope){
$scope.toggleLoad=function(){
$scope.beginLoading=!$scope.beginLoading;
};
}]); </script>
</html>

大家可以复制运行一下,提示:需要修改引入文件的路径。

这个指令功能很简单只是点击加入loading状态,如何不屑与这个功能,那就别往下看了,直接点赞吧,谢谢!

     指令这个东西,格式需要记住。

     下次首先讲讲”.directive()”,谢谢关注!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,965
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,782