当前位置: 首页 > 编程笔记 >

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

上官高逸
2023-03-14
本文向大家介绍Angularjs 创建可复用组件实例代码,包括了Angularjs 创建可复用组件实例代码的使用技巧和注意事项,需要的朋友参考一下

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/

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

 类似资料:
  • 本文向大家介绍vue19 组建 Vue.extend component、组件模版、动态组件 的实例代码,包括了vue19 组建 Vue.extend component、组件模版、动态组件 的实例代码的使用技巧和注意事项,需要的朋友参考一下 具体代码如下所示: 下面看下vue component动态组件  动态组件 通过component标签 的is属性来进行组件的切换 is的属性值决定要显示的

  • 问题内容: 我在网上搜索了可拖动的Swing组件的示例,但发现示例不完整或不起作用。 我需要的是一个 Swing组件 ,可以用鼠标将其 拖动 到另一个组件中。在拖动时,它应该 已经改变 了位置,而不仅仅是“跳转”到目的地。 我将感谢没有非标准API的示例。 谢谢。 问题答案: 我提出了一个简单但可行的解决方案,我自己找到了;) 我该怎么办? 当按下鼠标时,我 在屏幕上* 记录了 光标的 位置以及

  • 实例(Instance)是运行在云中的虚拟机。 在创建新实例之前,您要提前知晓以下参数: 实例的源可以是镜像,快照,或者包含镜像或快照的块存储设备。 实例的名字。 您实例的型号,这个型号决定了您nova实例的CPU,内存和磁盘空间情况。型号(flavor)是您虚拟机的硬件可用配置。它决定了您能创建的虚拟机的大小。 任意的用户数据文件。用户数据文件时在元数据服务中的一个特殊的键,它保存了一份能给虚拟

  • 完成初始设置并已登录 Navicat Monitor 后,即可创建要监控的实例。Navicat Monitor 使用无代理体系结构来监控数据库服务器并定期收集数据。它不需要在被监控的服务器上安装任何代理软件。 你可以在以下页面创建新实例,点击“+ 新建实例”并选择服务器类型。 概览 配置 在新建实例窗口中,在“实例名”输入一个恰当名称描述你的实例,并选择实例的“组”。如果你要添加新组,请点击“新建

  • 我正在使用room和ViewModel开发一个notes应用程序,但在初始化ViewModel内部活动时,我得到了以下信息: JAVAlang.RuntimeException:无法创建类包的实例。笔记。主屏幕。家信 这是我的主要活动 而ViewModel就像- 我的视图模型工厂就像- 我不知道问题出在哪里。帮助我。提前谢谢。 更新:-完整的stacktrace是

  • 本文向大家介绍使用C#创建Windows服务的实例代码,包括了使用C#创建Windows服务的实例代码的使用技巧和注意事项,需要的朋友参考一下 本文介绍了使用C#创建Windows服务的实例代码,分享给大家 一、开发环境 操作系统:Windows 10 X64 开发环境:VS2015 编程语言:C# .NET版本:.NET Framework 4.0 目标平台:X86 二、创建Windows Se