玩转angularJs——通过自定义ng-model,不仅仅只是input可以实现双向数据绑定
体验更优排版请移步原文:http://blog.kwin.wang/programming/angularJs-user-defined-ngmodel.html
angularJs双向绑定特性在开发中很方便很实用,但是由于ng-model一般只能挂在input上,因此我们需要自定义ng-model来在div等元素上使用该标签。
自定义指令:
//自定义ngModel的属性
.directive('contenteditable', ['$window', function() {
return {
restrict: 'A',
require: '?ngModel', // 此指令所代替的函数
link: function(scope, element, attrs, ngModel) {
if (!ngModel) {
return;
} // do nothing if no ng-model
// Specify how UI should be updated
ngModel.$render = function() {
element.html(ngModel.$viewValue || '');
};
// Listen for change events to enable binding
element.on('blur keyup change', function() {
scope.$apply(readViewText);
});
// No need to initialize, AngularJS will initialize the text based on ng-model attribute
// Write data to the model
function readViewText() {
var html = element.html();
// When we clear the content editable the browser leaves a <br> behind
// If strip-br attribute is provided then we strip this out
if (attrs.stripBr && html === '<br>') {
html = '';
}
ngModel.$setViewValue(html);
}
}
}
}])
页面中div可以这样使用ng-model:
<div class="icon-arrow-title title-color-2" contenteditable="true" ng-model="param.MobileNum" style="right: 15px;"></div>
这样,双向数据绑定就可以了。
玩转angularJs——通过自定义ng-model,不仅仅只是input可以实现双向数据绑定的更多相关文章
- 带你走近AngularJS - 创建自定义指令
带你走近AngularJS系列: 带你走近AngularJS - 基本功能介绍 带你走近AngularJS - 体验指令实例 带你走近AngularJS - 创建自定义指令 ------------- ...
- angularjs directive (自定义标签解析)
angularjs directive (自定义标签解析) 定义tpl <!-- 注意要有根标签 --> <div class="list list-inset" ...
- 一招制敌 - 玩转 AngularJS 指令的 Scope (作用域),讲得特别好
学习了AngularJS挺长时间,最近再次回首看看指令这部分的时候,觉得比自己刚开始学习的时候理解的更加深入了,尤其是指令的作用域这部分. 步入正题: 每当一个指令被创建的时候,都会有这样一个选择,是 ...
- AngularJS中自定义有关一个表格的Directive
本篇体验在AngularJS中自定义一个有关表格的Directive.表格的需求包括: ● 表格结构 <table> <thead> <tr> ...
- angular 中怎么获取路径上的参数 参考:https://docs.angularjs.org/api/ng/service/$location
参考: https://docs.angularjs.org/api/ng/service/$location
- combox使用自定义的model列表中无元素显示
自定义的model(stationModel)中有 name 和point两种属性名. 初始化stationModel Combobox{ textRole: 'name' model:station ...
- 玩转Spring Boot 自定义配置、导入XML配置与外部化配置
玩转Spring Boot 自定义配置.导入XML配置与外部化配置 在这里我会全面介绍在Spring Boot里面如何自定义配置,更改Spring Boot默认的配置,以及介绍各配置的优先 ...
- angularJs:双向数据绑定
示例1 <!DOCTYPE html> <html ng-app> <head> <meta charset="UTF-8" /> ...
- angularJs初体验,实现双向数据绑定!使用体会:比较爽
使用初体验:ng 双向数据绑定: 最简单的双向数据绑定:(使用默认模块控制) <body ng-app> <input type="text" ng-model= ...
随机推荐
- sql如何分组选择显示最新的一条数据
怎样在数据库的一个表里筛选出每一人的时间最新的一条记录?用SQL语句 wenchuan408 wenchuan408 结帖率:100% sql数据库 yhh name ...
- Idea_00_资源贴
一.精选资料 tengj/IntelliJ-IDEA-Tutorial IntelliJ IDEA 使用教程-极客学院 二.参考资料 eclipse&Myeclipse&Intelli ...
- 地图之CoreLocation
1. 在Info.plist文件中添加下面两句话 NSLocationAlwaysUsageDescription —> 确定定位吗?亲 (或者改参数类型为BOOL类型 值为Y) 请求的授权, ...
- UI-UIButton、UILable、UITextField总结
UIButton按钮====================================================== 第一.UIButton的定义 UIButton *button=[[U ...
- Wordpress在主题或者插件中自定义存储附件的方法
1.前端使用form表单 //单文件上传,我的业务需求中限制了必须上传图片 <input type="file" name="singlename" ac ...
- MySQL install and setting
Tomorrow is the deadline of DATABASE, I am very nervous because of my project. Today is first day th ...
- NOIP模拟题 序列
题目大意 给定长为$n$的序列$A$,定义长为$k$的区间中位数为从小到大排完序后第$\lfloor\frac{k}{2}\rfloor$个数的大小. 每次询问给定$l_1,r_1,l_2,r_2$有 ...
- JAVA如何以追加的方式向文件中写入信息?
以FileWriter类为例: FileWriter的构造方法中有一个方法是:FileWriter(String fileName, boolean append) ,其中第二个参数决定了写文件的方 ...
- js对象原型链
JavaScript 规定,每一个构造函数都有一个 prototype 属性,指向另一个对象.这个对象的所有属性和方法,都会被构造函数的所拥有. 这也就意味着,我们可以把所有对象实例需要共享的属性和方 ...
- Maven根据不同的环境打包不同的配置
前言: 在开发过程中,我们的软件会面对不同的运行环境,比如开发环境.测试环境.生产环境,而我们的软件在不同的环境中,有的配置可能会不一样,比如数据源配置.日志文件配置等等. 那么就需要借助maven提 ...