使用ng-model的AngularJS自定义表单组件/指令 [英] AngularJS custom form component / directive using ng-model

查看:56
本文介绍了使用ng-model的AngularJS自定义表单组件/指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用常规输入时,例如

<form name="myForm">
  <input type="text" ng-model="foobar">
</form>

在输入框myForm.$dirty中输入

为真.

after typing in the input box myForm.$dirty is true.

我想创建一个简单的指令,例如

I'd like to create a simple directive such as

angular.module('myModule', [])
.directive('myDirective', function() {
  return {
    restrict: 'E',
    scope: {
      fooBar: '='
    },
    template: '<div><button ng-click="fooBar=foo"></button><button ng-click="fooBar=bar"></button></div>'
  };
});

样品用量为

<form name="myForm">
  <my-directive foo-bar="myObj.foobarValue"></my-directive>
</form>

,并且在用户单击两个按钮中的任何一个之后,myForm$dirty设置为true.

and after user clicks on any of the two buttons, myForm$dirty is set to true.

这是如何完成的?

推荐答案

实施自定义表单控件(使用ngModel)

使用 ngModel控制器 require属性:

Implementing custom form controls (using ngModel)

Use the ngModel controller and the object form of the require property in the DDO:

angular.module('myModule', [])
.directive('myDirective', function() {
  return {
    restrict: 'E',
    require: { ngModelCtrl: 'ngModel' },
    scope: {
      ngModel: '<'
    },
    bindToController: true,
    controllerAs: '$ctrl',
    template: 
       `<div>
          <button ng-click="$ctrl.ngModelCtrl.$setViewValue('foo')">
              Set foo
          </button>
          <button ng-click="$ctrl.ngModelCtrl.$setViewValue('bar')">
              Set bar
          </button>
       </div>`,
    controller: function ctrl() {}
  };
});

用法:

<form name="myForm">
    <input type="text" ng-model="foobar">
    <my-directive ng-model="foobar"></my-directive>
</form>

通过实例化并使用 ng-model控制器,指令将根据需要自动设置表单控件.

By instantiating and using the ng-model controller, the directive will automatically set the form controls as necessary.

angular.module('myModule', [])
.directive('myDirective', function() {
  return {
    restrict: 'E',
    require: { ngModelCtrl: 'ngModel' },
    scope: {
      ngModel: '<'
    },
    bindToController: true,
    controllerAs: '$ctrl',
    template: 
       `<div>
          <button ng-click="$ctrl.ngModelCtrl.$setViewValue('foo')">
              Set foo
          </button>
          <button ng-click="$ctrl.ngModelCtrl.$setViewValue('bar')">
              Set bar
          </button>
       </div>`,
    controller: function ctrl() {}
  };
});

<script src="//unpkg.com/angular/angular.js"></script>
  <body ng-app="myModule">
    <h2>ngModel DEMO</h2>
    <form name="myForm">
        <input type="text" ng-model="foobar">
        <my-directive ng-model="foobar"></my-directive>
    </form>
    <br>myForm.$dirty = {{myForm.$dirty}}
    <br>myForm.$pristine = {{myForm.$pristine}}
    <br><button ng-click="myForm.$setDirty()">Set dirty</button>
    <br><button ng-click="myForm.$setPristine()">Set pristine</button>
  </body>

我建议使用ngModel作为输入隔离作用域.输出应使用 $ setViewValue方法.

I recommend isolate scope with ngModel as an input. Output should be done with the $setViewValue method.

有关更多信息,请参见

AngularJS开发人员指南-基于组件的应用程序体系结构

这篇关于使用ng-model的AngularJS自定义表单组件/指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆