下面代码实测通过,直接copy到本地运行即可。

<!DOCTYPE html>
<html> <head>
<meta charset="UTF-8">
<title></title>
<script src="https://cdn.bootcss.com/angular.js/1.2.14/angular.min.js"></script>
<style>
.yf-input-container {
position: relative;
font-size: 14px;
margin-bottom: 10px;
} .yf-input-container input {
height: 20px;
line-height: 20px;
}
</style>
</head> <body>
<div ng-controller="superPoolCtrl">
<div class="yf-input-container">
姓名:<input type="text" ng-model="name" yf-placeholder="请输入姓名" />
</div>
<div class="yf-input-container">
电话:<input type="text" ng-model="tel" yf-placeholder="请输入电话" />
</div>
<div>
<div class="yf-input-container">
职位:<input type="text" ng-model="job" yf-placeholder="请输入职位" />
</div>
</div>
</div>
<script>
var moduleName = 'bidmeApp';
var app = angular.module(moduleName, [])
.controller('superPoolCtrl', ['$scope', '$rootScope', '$timeout', function($scope, $rootScope, $timeout) {
$timeout(function() {
$scope.tel = "13800138000";
}, 1000);
$scope.name = "http://www.cnblogs.com/chenchenghua/";
}])
.directive('yfPlaceholder', function() {
return {
restrict: 'A', //指令取属性
scope: false, //与父级共享作用域
replace: true,
require: '',
template: function(elem, attr) {
return '<input />'
},
link: function(scope, element, attr) {
var inputEle = element[0];
var supportPlaceholder = "placeholder" in document.createElement("input") ? true : false;
if(supportPlaceholder) {
inputEle.setAttribute('placeholder', attr.yfPlaceholder);
} else {
function insertAfter(newElement, targetElement) { // newElement是要追加的元素 targetElement 是指定元素的位置
var parent = targetElement.parentNode; // 找到指定元素的父节点
if(parent.lastChild == targetElement) { // 判断指定元素的是否是节点中的最后一个位置 如果是的话就直接使用appendChild方法
parent.appendChild(newElement, targetElement);
} else {
parent.insertBefore(newElement, targetElement.nextSibling);
};
};
var node = angular.element(document.createElement('span'));
node.css({
position: 'absolute',
zIndex: '1',
color: '#A9A9A9',
top: inputEle.offsetTop + 'px',
left: inputEle.offsetLeft + 'px',
width: inputEle.offsetWidth + 'px',
height: inputEle.offsetHeight + 'px',
lineHeight: inputEle.offsetHeight + 'px',
textIndent: '5px',
cursor: 'text'
}).text(attr.yfPlaceholder);
angular.element(inputEle).after(node); node.on('click', function(a, b, c) { //点击placeholder,隐藏。input获取焦点
node.css({
"display": 'none'
});
inputEle.focus();
}); angular.element(inputEle).on('blur', function() { //input失去焦点时,做判断
if(inputEle.value.length < 1) {
node.css({
"display": 'block'
});
}
}); angular.element(inputEle).on('focus', function() { //input失去焦点时,做判断
if(inputEle.value.length < 1) {
node.css({
"display": 'none'
});
}
}); scope.$watch(attr.ngModel, function(newVal, oldVal) { //监听,比如开始有值或者异步请求回显,placeholder隐藏
if(!!newVal && newVal.length > 0) {
node.css({
"display": 'none'
});
}
});
}
}
};
});
angular.bootstrap(document.body, [moduleName]);
</script>
</body> </html>

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

欢迎转载,转载请注明作者:飘飞的夏秋 和出处 http://www.cnblogs.com/chenchenghua/p/6757736.html

最新文章

  1. linux实践之程序破解
  2. iOS里防止按钮被多次点击的办法
  3. Web性能测试基本指标
  4. 用通俗易懂的大白话讲解Map/Reduce原理
  5. (转)JQuery处理json与ajax返回JSON实例
  6. RestTemplate 发送 get 请求使用误区 多个参数传值为null(转载)
  7. day 17 - 2 递归函数练习
  8. django中如何实现分页功能
  9. phxpaxos遇到反复拉取checkpoint但是反复失败的问题,给其它节点造成压力
  10. 2018.10.2浪在ACM 集训队第三次测试赛
  11. gulp 编译es6 react 教程 案例 配置
  12. android的logcat详细用法!
  13. const修饰的成员是类成员,还是实例成员?
  14. Hadoop生态圈-hive优化手段-作业和查询优化
  15. Win8Metro(C#)数字图像处理--2.40二值图像轮廓提取
  16. TypeScript在node项目中的实践
  17. python接口自动化21-下载excel文件(Content-Type:octets/stream)
  18. Ubuntu下编译Hello World驱动并运行全过程
  19. 关于function的一种常用用法
  20. Cesium加载三维倾斜摄影数据

热门文章

  1. Oracle 常用语句备份
  2. Qt4.8.5移植
  3. APP开发的三种技术对比
  4. Mysql+innodb数据存储逻辑
  5. 远控项目(Windows Socket)
  6. 我该不该学习C语言
  7. PHP学习2——基本语法
  8. C#基础知识-使用XML完成一个小程序(十一)
  9. Java - USC2字符串截取
  10. 使用 typeof 来检测对象是否undefined