我们知道ngModel是AngularJS中默认的一个Directive,用于数据的双向绑定。通常是这样使用的:

<input type="text" ng-model="customer.name" />

在控制器中大致这样:

$scope.customer ={
    name: ''
}

上一篇中,有关表格的Directive是这样使用的:

<table-helper datasource="customers" clumnmap="[{name: 'Name'}, {street: 'Street'}, {age: 'Age'}, {url: 'URL', hidden: true}]"></table-helper>

以上,datasource代表数据源,是否可以用ng-model替代呢?

比如写成这样:

<table-helper-with-ng-model ng-model="customers" columnmap="[{name:'Name'}...]">
</table-helper-with-ng-model>

可是,自定义的tableHelper这个Direcitve如何获取到ngModel中的值呢?

这就涉及到Direcitve之间的通讯了,就像在"AngularJS中Directive间交互实现合成"说的。

要解决的第一个问题是:如何拿到ngModel?

--使用require字段

return {
restrict: 'E',
required: '?ngModel', //^ngModel本级或父级, ^^ngModel父级
scope: {
columnmap: '='
},
link: link,
template: template
}

要解决的第二个问题是:如何从ngModel这个Direcitve拿数据?

--使用ngModel.$modelValue

要解决的的第三个问题是:当ngModel值变化,如何告之外界并重新加载表格?

--大致有4种方法

//1 观察某个属性的值
attrs.$observe('ngModel', function(value){
//监视变量的值
scope.$watch(value, function(newValue){
render();
});
}); //2 或者
scope.$watch(attrs.ngModel, render); //3 或者
scope.$watch(function(){
return ngModel.$modelValue;
}, function(newValue){
render();
}) //4 或者
ngModel.$render = function(){
render();
}

相对完整的代码如下:

var tableHelperWithNgModel = function(){

    var dataSource;

    var template = '<div class="tableHelper"></div>';

    var link = function(scope, element, attrs, ngModel){

        ...

        function render(){
if(ngModel && ngModel.$modelValue.length){
datasource = ngModel.$modelValue;
table += tableStart;
table += renderHeader();
table += renderRows() + tableEnd;
renderTable();
}
}
}; return {
restrict: 'E',
required: '?ngModel', //^ngModel本级或父级, ^^ngModel父级
scope: {
columnmap: '='
},
link: link,
template: template
}
} angular.module('direcitveModule')
.directive('tableHelperWithNgModel', tableHelperWithNgModel) var tableHelperWithNgModel = function(){ var dataSource; var template = '<div class="tableHelper"></div>'; var link = function(scope, element, attrs, ngModel){ //观察某个属性的值
attrs.$observe('ngModel', function(value){
//监视变量的值
scope.$watch(value, function(newValue){
render();
});
}); //或者
scope.$watch(attrs.ngModel, render); //或者
scope.$watch(function(){
return ngModel.$modelValue;
}, function(newValue){
render();
}) //或者
ngModel.$render = function(){
render();
} function render(){
if(ngModel && ngModel.$modelValue.length){
datasource = ngModel.$modelValue;
table += tableStart;
table += renderHeader();
table += renderRows() + tableEnd;
renderTable();
}
}
}; return {
restrict: 'E',
required: '?ngModel', //^ngModel本级或父级, ^^ngModel父级
scope: {
columnmap: '='
},
link: link,
template: template
}
} angular.module('direcitveModule')
.directive('tableHelperWithNgModel', tableHelperWithNgModel)

自定义Directive使用ngModel的更多相关文章

  1. 理解AngularJS生命周期:利用ng-repeat动态解析自定义directive

    ng-repeat是AngularJS中一个非常重要和有意思的directive,常见的用法之一是将某种自定义directive和ng-repeat一起使用,循环地来渲染开发者所需要的组件.比如现在有 ...

  2. AngularJS自定义Directive

    (编辑完这篇之后,发现本篇内容应该属于AngularJS的进阶,内容有点多,有几个例子偷懒直接用了官方的Demo稍加了一些注释,敬请见谅). 前面一篇介绍了各种常用的AngularJS内建的Direc ...

  3. 关于angular 自定义directive

    关于angular 自定义directive的小结 首先我们创建一个名为"expander"的自定义directive指令: angular.module("myApp& ...

  4. AngularJs中,如何在父元素中调用子元素为自定义Directive中定义的函数?

    最近一段时间准备使用AngularJs中的自定义Directive重构一下代码. 在这里说明一下,把自定义控件封装成Directive并不一定是要复用,而是要让代码结构更加清晰.就好像你将一个长方法拆 ...

  5. AngularJS自定义Directive中link和controller的区别

    在AngularJS中,自定义Directive过程中,有时用link和controller都能实现相同的功能.那么,两者有什么区别呢? 使用link函数的Directive 页面大致是: <b ...

  6. AngularJS自定义Directive不一定返回对象

    AngularJS中,当需要自定义Directive时,通常返回一个对象,就像如下的写法: angular.module('modulename') .directive('myDirective', ...

  7. AngularJS自定义Directive初体验

    通常我们这样定义个module并随之定义一个controller. var app = angular.module('myApp', []); app.controller('CustomersCo ...

  8. AngularJs(Part 11)--自定义Directive

    先对自定义Directive有一个大体的映像 myModule.directive('myDirective',function(injectables){ var directiveDefiniti ...

  9. angularJs中自定义directive的数据交互

    首先放官方文档地址:https://docs.angularjs.org/guide/directive 就我对directive的粗浅理解,它一般用于独立Dom元素的封装,应用场合为控件重用和逻辑模 ...

随机推荐

  1. tomcat6和tomcat7管理用户manager配置

    tomcat用户登录文件配置 如果想要对部署在tomcat上的项目进行管理查看,需要在tomcat安装目录conf文件夹下的tomcat-user.xml里添加用户登录权限.具体添加的内容如下: To ...

  2. adb devices检测不到夜神模拟器

    1.dos下,cd进入到夜神模拟器的bin目录 代码: nox_adb connect 127.0.0.1:62001 2.dos下,进入进Android SDK下的platform-tools目录 ...

  3. vue和echarts 封装的 v-charts 图表组件

    https://v-charts.js.org/#/ 在使用 echarts 生成图表时,经常需要做繁琐的数据类型转化.修改复杂的配置项,v-charts 的出现正是为了解决这个痛点.基于 Vue2. ...

  4. LeetCode(39):组合总和

    Medium! 题目描述: 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates ...

  5. LeetCode(32):最长有效括号

    Hard! 题目描述: 给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. 示例 1: 输入: "(()" 输出: 2 解释: 最长有效括号子串为 ...

  6. system

    system("cls"); //清屏 system("color f2") //改变控制台颜色 f2为颜色样式,可以是e2.f3等等 Original:htt ...

  7. 存储过程+Jquery+WebService实现三级联动:

    首先看一下数据库的设计:

  8. 微信支付支付宝支付生成二维码的方法(php生成二维码的三种方法)

    如果图简单,可以用在线生成 http://pan.baidu.com/share/qrcode?w=150&h=150&url=http://www.xinzhenkj.com 最简单 ...

  9. springboot项目连接数据库报错

    学习SpringBoot也没有多久,今天SpringBoot连接数据库的时候报如下错误: java.sql.SQLException: The server time zone value '�й�� ...

  10. MVC常用筛选器Filter

    1.ActionFilterAttribute using System; using System.Collections.Generic; using System.Diagnostics; us ...