我们知道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. Javascript中Json对象与Json字符串互相转换方法汇总(4种转换方式)

    1.Json对象转Json字符串 JSON.stringify(obj); 2.Json字符串传Json对象 JSON.parse(str);//第一种 $.parseJSON(str);//第二种, ...

  2. Windows 2012 安装 Oracle 11g 报错:[INS-13001]环境不满足最低要求。

    问题: 在Windows Server 2012 安装上 Oracle 11g 时,安装程序报错:[INS-13001]环境不满足最低要求. 分析原因: Oracle 在发布 11g时,Winodws ...

  3. 2017-05~06 温故而知新--NodeJs书摘(一)

    前言: 毕业到入职腾讯已经差不多一年的时光了,接触了很多项目,也积累了很多实践经验,在处理问题的方式方法上有很大的提升.随着时间的增加,愈加发现基础知识的重要性,很多开发过程中遇到的问题都是由最基础的 ...

  4. Git 将项目(代码)从GitHub上克隆(下载)到本地仓库

    要将项目从GitHub上克隆到本地,首先你得下载并安装好git for window. 配置Git: 1.安装完后,右键单击桌面空白处,选择Git Gui Here,进去之后,选择左上角的help选项 ...

  5. LeetCode(27): 移除元素

    Easy! 题目描述: 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1 ...

  6. hdu2289二分答案 圆台体积

    精度小一点就能过 #include<bits/stdc++.h> #define maxn 1000000009 #define esp 1e-9 #define PI 3.1415926 ...

  7. GDIPlus非典型误用一例

    // ** 初始化GDI+ Gdiplus::GdiplusStartupInput gdiplusStartupInput; // ** 该成员变量用来保存GDI+被初始化后在应用程序中的GDI+标 ...

  8. 安卓在代码中设置TextView的drawableLeft、drawableRight、drawableTop、drawableBottom

    Drawable rightDrawable = getResources().getDrawable(R.drawable.icon_new); //调用setCompoundDrawables时, ...

  9. DevExpress学习笔记之如何获取Repository Item的值

    上一章我们看到了如何在TreeList的单元格中动态绑定不同的控件,此类控件被称为In_Place Control.所谓“In_Place”,我的理解为“内置”控件,既然有“内”就相对的会有“外”,其 ...

  10. Codeforces Round #369 (Div. 2)-C Coloring Trees

    题目大意:有n个点,由m种颜料,有些点没有涂色,有些点已经涂色了,告诉你每个点涂m种颜色的价格分别是多少, 让你求将这n个点分成k段最少需要多少钱. 思路:动态规划,我们另dp[ i ][ j ][ ...