Angularjs 创建可复用组件实例代码


Posted in Javascript onOctober 09, 2016

AngularJS框架可以用Service和Directive降低开发复杂性。这个特性非常适合用于分离代码,创建可测试组件,然后将它们变成可重用组件。

Directive是一组独立的JavaScript、HTML和CSS,它们封装了一个特定的行为,它将成为将来创建的Web组件的组成部分,我们可以在各种应用中重用这些组件。在创建之后,我们可以直接通过一个HTML标签、自定义属性或CSS类、甚至可以是HTML注释,来执行一个Directive。

这一篇教程将介绍如何创建一个‘自定义步长选择' Directive,它可以作为一个可重用输入组件。本文不仅会介绍Directive的一般创建过程,还会介绍输入控件验证方法,以及如何使用ngModelController无缝整合任意表单,从而利用AngularJS表单的现有强大功能。

直接上代码:

html:

<!-- lang: html -->
<body ng-app="demo" ng-controller="DemoController">
  <form name="form" >
    Model value : <input type="text" size="3" ng-model="rating"><br>
    Min value: <input type="text" size="3" ng-model="minRating"><br>
    Max value: <input type="text" size="3"ng-model="maxRating"><br>
    Form has been modified : {{ form.$dirty }}<br>
    Form is valid : {{ form.$valid }}
    <hr><divmin="minRating"max="maxRating"ng-model="rating"rn-stepper></div></form></body>

js:

<!-- lang: js -->
angular.module(‘demo‘, [
  ‘revolunet.stepper‘
])

.controller(‘DemoController‘, function($scope) {
  $scope.rating = 42;   
  $scope.minRating = 40;
  $scope.maxRating = 50;
});

rn-stepper最简结构

<!-- lang: js -->
// we declare a module name for our projet, and its dependencies (none)
angular.module(‘revolunet.stepper‘, [])
// declare our naïve directive
.directive(‘rnStepper‘, function() {
  return {
    // can be used as attribute or element
    restrict: ‘AE‘,
    // which markup this directive generates
    template: ‘<button>-</button>‘ +
            ‘<div>0</div>‘ +
            ‘<button>+</button>‘
  };
});

现在directive rnStepper 已经有了一个简单的雏形了。

可以有如下两种试用方法:

<div rn-stepper> </div>
<rn-stepper> </rn-stepper>

demo: http://jsfiddle.net/revolunet/n4JHg/

添加内部动作

直接上代码:

<!-- lang: js -->
.directive(‘rnStepper‘, function() {
  return {
    restrict: ‘AE‘,

    // declare the directive scope as private (and empty)
    scope: {},

    // add behaviour to our buttons and use a variable value
    template:
        ‘<button ng-click="decrement()">-</button>‘ +
        ‘<div>{{value}}</div>‘ +
        ‘<button ng-click="increment()">+</button>‘,

    // this function is called on each rn-stepper instance initialisation
    // we just declare what we need in the above template
    link: function(scope, iElement, iAttrs) {
      scope.value = 0;
      scope.increment = function() {
        scope.value++;
      };
      scope.decrement = function() {
        scope.value--;
      };
    }
  };
});

我们在template中,分别给两个button添加了click事件响应,在link方法中实现了响应的方法。
这里的scope是一个private scope,其作用域仅限rnStepper这个directive。

demo: http://jsfiddle.net/revolunet/A92Aw/

与外部世界(外部作用域)的交互

直到上面为止,我们的rnStepper都是自己跟自己玩,并没有跟外部作用域进行一些交互。

下面我们将添加一个数据绑定,使rnStepper与外部世界建立联系。

直接上代码:

<!-- lang: js -->
scope: {
  value: ‘=ngModel‘
}

我们在scope中添加了一组键值对,这样,会自动建立内部变量value与外部属性ngModel的联系。
这里的=代表的意思是双向绑定(double data-binding)。

什么叫双向绑定?

即: 当value发生改变,那么ngModel也会发生改变,反之亦然。

在我们的这个demo中,看下面这行代码:

<!-- lang: js -->
<div rn-stepper ng-model="rating"></div>

这里的意思就是: directive rnStepper的内部变量value与外部scope中的rating建立了双向数据绑定。

demo: http://jsfiddle.net/revolunet/9e7Hy/

让我们组件更加友好

直接上代码:

<!-- lang: js -->
.directive(‘rnStepper‘, function() {
  return {
    // restrict and template attributes are the same as before.
    // we don‘t need anymore to bind the value to the external ngModel
    // as we require its controller and thus can access it directly
    scope: {},
    // the ‘require‘ property says we need a ngModel attribute in the declaration.
    // this require makes a 4th argument available in the link function below
    require: ‘ngModel‘,
    // the ngModelController attribute is an instance of an ngModelController
    // for our current ngModel.
    // if we had required multiple directives in the require attribute, this 4th
    // argument would give us an array of controllers.
    link: function(scope, iElement, iAttrs, ngModelController) {
      // we can now use our ngModelController builtin methods
      // that do the heavy-lifting for us

      // when model change, update our view (just update the div content)
      ngModelController.$render = function() {
        iElement.find(‘div‘).text(ngModelController.$viewValue);
      };

      // update the model then the view
      function updateModel(offset) {
        // call $parsers pipeline then update $modelValue
        ngModelController.$setViewValue(ngModelController.$viewValue + offset);
        // update the local view
        ngModelController.$render();
      }

      // update the value when user clicks the buttons
      scope.decrement = function() {
        updateModel(-1);
      };
      scope.increment = function() {
        updateModel(+1);
      };
    }
  };
});

这里,我不在需要内部变量value了。因为我们在link方法中已经拿到了ngModelController的引用,这里的ngModelController.$viewValue其实就是前面例子中的value。

但是我们又添加了另外一组键值对require: ‘ngModel‘。

我们使用了两个新的API:

ngModelController.$render: 在ngModel发生改变的时候框架自动调用,同步$modelValue和$viewValue, 即刷新页面。

ngModelController.$setViewValue: 当$viewValue发生改变时,通过此方法,同步更新$modelValue。
demo: http://jsfiddle.net/revolunet/s4gm6/

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

Javascript 相关文章推荐
javascript编程起步(第四课)
Feb 27 Javascript
javascript对select标签的控制(option选项/select)
Jan 31 Javascript
JavaScript+CSS无限极分类效果完整实现方法
Dec 22 Javascript
jQuery插件imgPreviewQs实现上传图片预览
Jan 15 Javascript
Ionic默认的Tabs模板使用实例
Aug 29 Javascript
Vue.js如何优雅的进行form validation
Apr 07 Javascript
JavaScript模块详解
Dec 18 Javascript
使用vue中的v-for遍历二维数组的方法
Mar 07 Javascript
微信小程序如何获取用户收货地址
Nov 27 Javascript
JS Generator 函数的含义与用法实例总结
Apr 08 Javascript
Javascript查看大图功能代码实现
May 07 Javascript
JS获取一个字符串中指定字符串第n次出现的位置
Feb 10 Javascript
Boostrap实现的登录界面实例代码
Oct 09 #Javascript
深入理解bootstrap框架之第二章整体架构
Oct 09 #Javascript
javascript 判断是否是微信浏览器的方法
Oct 09 #Javascript
深入理解bootstrap框架之入门准备
Oct 09 #Javascript
微信小程序 http请求详细介绍
Oct 09 #Javascript
微信小程序 Flex布局详解
Oct 09 #Javascript
JavaScript实现Java中Map容器的方法
Oct 09 #Javascript
You might like
php 保留字列表
2012/10/04 PHP
使用PHP实现阻止用户上传成人照片或者裸照
2014/12/25 PHP
THINKPHP在添加数据的时候获取主键id的值方法
2017/04/03 PHP
PHP curl 或 file_get_contents 获取需要授权页面的方法
2017/05/05 PHP
使用laravel根据用户类型来显示或隐藏字段
2019/10/17 PHP
用cookies实现的可记忆的样式切换效果代码下载
2007/12/24 Javascript
jquery 新手学习常见问题解决方法
2010/04/18 Javascript
jQuery学习笔记[1] jQuery中的DOM操作
2010/12/03 Javascript
nodejs 后缀名判断限制代码
2011/03/31 NodeJs
js 关键词高亮(根据ID/tag高亮关键字)案例介绍
2013/01/21 Javascript
JavaScript中逗号运算符介绍及使用示例
2015/03/13 Javascript
如何利用AngularJS打造一款简单Web应用
2015/12/05 Javascript
Javascript将双字节字符转换成单字节字符并计算长度
2016/06/22 Javascript
javascript实现秒表计时器的制作方法
2017/02/16 Javascript
Vue监听数组变化源码解析
2017/03/09 Javascript
原生JS实现的跳一跳小游戏完整实例
2019/01/27 Javascript
浅谈layer的Icon样式以及一些常用的layer窗口使用方法
2019/09/11 Javascript
JavaScript实现拖拽和缩放效果
2020/08/24 Javascript
利用PHP实现递归删除链表元素的方法示例
2020/10/23 Javascript
vue实现可移动的悬浮按钮
2021/03/04 Vue.js
[06:14]《辉夜杯》外卡赛附加赛 4支战队巡礼
2015/10/23 DOTA
python实现问号表达式(?)的方法
2013/11/27 Python
python正则表达式及使用正则表达式的例子
2018/01/22 Python
python根据txt文本批量创建文件夹
2020/12/08 Python
详解python校验SQL脚本命名规则
2019/03/22 Python
Python利用WMI实现ping命令的例子
2019/08/14 Python
tensorflow的ckpt及pb模型持久化方式及转化详解
2020/02/12 Python
DRF使用simple JWT身份验证的实现
2021/01/14 Python
唤醒头发毛囊的秘密武器:Grow Gorgeous
2016/08/28 全球购物
沃尔玛加拿大:Walmart.ca
2020/03/02 全球购物
世界上最大的艺术社区:SAA
2020/12/30 全球购物
大学生简历的个人自我评价
2013/12/04 职场文书
信息系统专业个人求职信范文
2013/12/07 职场文书
党的群众路线教育实践活动学习笔记范文
2014/11/06 职场文书
慈善献爱心倡议书
2015/04/27 职场文书
大学军训口号大全
2015/12/24 职场文书