You can use ngModel in your own directives, but there are a few things you'll need to do to get it working properly.

ngModel itself is an directive. If you want to use it inside your own directive, you should use require keyword.

/**
* Created by Answer1215 on 12/18/2014.
*/
angular.module('app', [])
.directive('bank', function() {
return{
restrict: 'E',
template: '<div>Click me to add $10 into your account</div>',
require: 'ngModel',
//The ^ prefix means that this directive searches for the controller on its parents (without the ^ prefix, the directive would look for the controller on just its own element)
link: function(scope, element, attrs, ngModelCtrl) {
//so it looks for the controller on ngModel --> ngModelController
//it has method setViewValue
//has prop: viewValue
element.on('click', function() {
ngModelCtrl.$setViewValue(ngModelCtrl.$viewValue + 10);
scope.$apply();
})
}
}
})
<!DOCTYPE html>
<html ng-app="app">
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div ng-init="money=10"></div>
<bank ng-model="money"></bank>
{{money | currency}}
<script src="bower_components/angular/angular.min.js"></script>
<script src="app.js"></script>
</body>
</html>

最新文章

  1. UI复习
  2. CSS3-animation,表格表单的格式化
  3. Android EditText 密码框默认是小圆点 怎么改成其它的(*)?
  4. 在autoit中如何将combobox设置为只允许选择不允许输入呢
  5. 实现毛玻璃模糊效果/DRNRealTimeBlur
  6. Yii 框架里数据库操作详解
  7. UIVIew之霓虹灯实现
  8. amcharts报表制作
  9. mime大全收集
  10. GoLang获取struct的tag
  11. 多线程异步编程示例和实践-Thread和ThreadPool
  12. C# xml 读xml、写xml、Xpath、Xml to Linq、xml添加节点 xml修改节点
  13. su与su -的区别
  14. linux crontab定时器
  15. mysql left join 查询
  16. HDU1072 Nightmare(BFS) 2016-07-24 14:02 40人阅读 评论(0) 收藏
  17. __iter__的有无
  18. Sqlite数据库初步的了解
  19. c/c++指针详解(一)
  20. 剑指Offer——第一个只出现一次的字符位置

热门文章

  1. HDU 5818 Joint Stacks
  2. Oracle OCI-22053:溢出错误解决方法
  3. android电池信息简介
  4. NiuTrans 日记 1
  5. Android 在安装完成界面,点击打开应用程序。在应用程序点击home键,再从桌面打开程序导致产生多个实例或者说程序被重复打开
  6. C++ 编程输入输出语句
  7. ios游戏开发--cocos2d学习(2)
  8. 关于“心脏出血”漏洞(heartbleed)的理解
  9. 团 大连网赛 1007 Friends and Enemies
  10. sf空间配置