我们知道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)

最新文章

  1. CSharpGL(19)用glReadPixels把渲染的内容保存为PNG图片(C#)
  2. HTML 学习笔记 JavaScript(面向对象)
  3. “You couldn’t see my tears cause I am in the water.“ Fish said to water.“But I could feel your tears cause you are in my heart..“ Answered water.
  4. AspectJ获取方法注解的信息
  5. vi 编辑器常用命令
  6. nyoj 92 图像有用区域
  7. 包装类(Wrapper Class)
  8. UI5_HomeWorkCompanyViewController
  9. codeforces 687C - The Values You Can Make 简单dp
  10. javaScript-什么是变量?
  11. NEST 中的协变
  12. Mapreduce 框架解析
  13. Linux(Ubuntu)换apt-get源
  14. java实现sftp客户端上传文件夹的功能
  15. 群晖IP地址更新问题
  16. BZOJ4860 BJOI2017 树的难题 点分治、线段树合并
  17. 轻松学C#----第二篇笔记
  18. [转]阿里云CentOS 7.1使用yum安装MySql5.6.24
  19. 简单DP入门四连发
  20. 尚硅谷STRUTS2视频学习笔记

热门文章

  1. IntelliJ IDEA 12:
  2. python恶俗古风诗自动生成器
  3. poj2342 没有上司的舞会 树形dp基础
  4. python 全栈开发,Day14(列表推导式,生成器表达式,内置函数)
  5. 使用super调用父类的构造方法
  6. Mac下Eclipse读取不到环境变量
  7. 20165203 《网络对抗技术》week1 Kali的安装与配置
  8. Maven多模块项目
  9. 修改idea自动生成在C盘的文件路径,以免电脑越用越卡
  10. Ubuntu 下常用快捷键