我想使用一个用ng-model和ng-required修饰的标准输入控件,然后添加我自己的自定义属性指令,为控件提供uib-typeahead功能.

我用这个链接让我的指令部分工作.

Add directives from directive in AngularJS

PLUNKR – The Version 2 of the directive does not work correctly with ng-model

我的指令确实添加了typeahead功能并且运行良好,但是在选择项目后它不会将模型绑定到控件上.

我有两个版本的指令.

版本1:是一个元素样式指令,我已经成功使用了一段时间,但是当我不想对输入元素进行更多控制时,它就失败了,特别是当我想使用ng-required =’时true’和其他ng-message指令.

版本2:是一个属性样式指令,我接受了这个,因为我觉得最好只添加我想要的任何标准HTML的typeahead功能,可以选择使用ng-required =’true’,ng-model等. .

虽然这个指令主要起作用,但是它与ng-model没有正确交互,我不知道如何让它工作

angular.module(APP)

.directive('wkLocationSuggest',['$compile',function ($compile) {
  return {
    restrict: 'A',require: 'ngModel',replace: false,//terminal: true,//priority: 0,scope: {
      wkApiModel: '=' // Provide access to the internal data that is returned via the API lookup
    },controller: 'LocationSuggestController',link: function (scope,element,attrs,ngModelCtrl) {
      if (!ngModelCtrl) {
        return;
      }

      element.attr('typeahead','location as row.location for row in typeAhead($viewValue)');
      element.attr('typeahead-wait-ms','750');
      element.attr('typeahead-on-select','onSelectInternal($item,$model,$label)');
      element.attr('typeahead-min-length','2');
      element.attr('typeahead-focus-first','true');

      element.removeAttr("wk-location-suggest");        //remove the location-suggest to avoid indefinite loop
      element.removeAttr("data-wk-location-suggest");   //also remove the same attribute with data- prefix if it exists

      // None of this is working
      //// invoked when model changes from the outside
      //ngModelCtrl.$render = function () {
      //  //scope.innerModel = ngModelCtrl.$modelValue;
      //};

      ////// invoked when model changes from the inside
      //scope.onChange = function (value) {
      //  ngModelCtrl.$setViewValue(scope.innerModel);
      //};

      scope.onSelectInternal = function ($item,$label) {

        // This fires,but it effects the ng-model on the first input,// but not the input that this directive is attached too
        ngModelCtrl.$setViewValue($item.location);

      };

      $compile(element)(scope);

    }
  };
}]);

这两个图像展示了部分问题,可能更好地使用上面的PLUNKR来测试自己

解决方法

我最初试图通过结合ngModel的$setValidity方法在input元素上实现blur来动态地将验证器添加到你的wk-location-suggest-new指令中;但不知道究竟是什么阻止了事件的发射.

因此,我转向另一个指令wk-location-suggest-old并稍微调整一下以适应两种期望的行为.

There,I noticed that you were missing a couple of things:

  • First of all,in order for a form element to glue with the form itself (wkProfileCompany in your case),and to work with ng-model,the element (in the directive template) needs a name.
  • Secondly,ng-required (or required) would work with the form only if it is added as an attribute to the element in the directive template,not the directive which compiles to the template containing the element.

指令定义

您可能已经注意到,我已经将外部作用域中的两个属性传递给指令的内部作用域,即:

>输入元素的名称,
>和一个isrequired标志,用于指定是否需要输入.

.

.directive('wkLocationSuggestOld',[function () {
  return {
    restrict: 'E',require: '?ngModel',scope: {
      name: '@',// <==
      isrequired: '=' // <==
    },template: '<input name="{{name}}" type="text" class="{{innerClass}}" ng-model="innerModel"'
       + ' ng-change="onChange()" uib-typeahead="location as row.location for row in typeAhead($viewValue)" '
       + ' typeahead-wait-ms="750" typeahead-on-select="onSelectInternal($item,$label)" '
       + ' typeahead-min-length="2" typeahead-focus-first="true" '
       + ' ng-required="isrequired">',// <== added ng-required here
    controller: 'LocationSuggestController',ngModel) {
      if (!ngModel) {
          return;
      }          
      ...
}])

HTML

最后,您可以在HTML中使用tweaked指令:

<wk-location-suggest-old class="form-control" type="text" name="location2" ng-model="location2" is-required="true"></wk-location-suggest-old>

Plunker

更新

ng-model没有在wk-location-suggest-new指令中正确绑定到提供的值(即location3)的一个可能原因是你要用一个新的自定义DOM元素替换整个DOM元素,该元素是用指令本身的孤立范围.

由于指令wk-location-suggest-new具有隔离范围,因此范围完全不知道location3,因为location3(以及所有其他位置值)是在MainCtrl的范围内定义的,而不是指令本身的范围;因此,您最终会将输入值绑定到未定义的属性.

link: function (scope,ngModelCtrl) {
   if (!ngModelCtrl) {
     return;
   }
 ...
$compile(element)(scope); // <== here

javascript – 将自定义指令添加到已具有角度指令的现有输入[ng-model / ng-required]的更多相关文章

  1. 使用layui实现左侧菜单栏及动态操作tab项的方法

    这篇文章主要介绍了使用layui实现左侧菜单栏及动态操作tab项的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  2. 在iOS上绘制扭曲的文本

    使用iOS9及更高版本中提供的标准API,如何在绘制文本时实现扭曲效果?

  3. ios – 如果Element符合给定的协议,则扩展阵列以符合协议

    如果是这样,语法是什么?解决方法Swift4.2在Swift4.2中,我能够使用符合这样的协议的元素扩展数组:

  4. ios – 如何在swift中获取2数组的常见元素列表

    (双关语)编辑:,你可以这样做这个实现是丑陋的.

  5. Swift 函数Count,Filter,Map,Reduce

    Count-统计数量文档示例Filter-条件过滤文档示例-过滤长度大于4的字符串也可以简化Map-映射集合类型,返回数组文档示例同样可以简化Reduce-把数组结合到一起文档示例可以简化进一步简化

  6. Swift语法——Swift Sequences 探究

    今天看到Array的API中有这么一个声明的函数:函数名为extend,所需参数是S类型的newElements,而S首先要实现SequenceType协议。看看APTGeneratorType必须要实现一个函数next(),它的作用就是返回一个Element,注释里说的很清楚:它的作用就是一直返回元素,直到最后。1)Swift调用generate()来生成了一个Generator,这个对象是一个私有的变量即__g;2)__g调用了next()函数,返回了一个optional类型对象element?。这个

  7. Swift 中数组和链表的性能

    尽管如此,我觉得链表的例子非常有意思,而且值得实现和把玩,它有可能会提升数组reduce方法的性能。同时我认为Swift的一些额外特性很有趣:比如它的枚举可以灵活的在对象和具体方法中自由选择,以及“默认安全”。这本书未来的版本可能就会用Swift作为实现语言。拷贝数组消耗的时间是线性的。使用链表还有其他的代价——统计链表节点的个数所需要的时间是统计数组元素个数时间的两倍,因为遍历链表时的间接寻址方式是需要消耗时间的。

  8. Swift中集合类型indexOf(Element)提示错误的解决办法

    简单的竟然出错了!其实看一下错误描述,大概就可以猜到Swift此时不知道你自定义类是如何比较的,如果是Swift内置的各种struct和class就不存在这个问题,比如:解决很简单,添加一个==方法即可:最后补充一下,早期版本的Swift还有一个find函数可以完成类似的功能,但是新版本已经没有该函数了,So你懂的…

  9. swift map reduce 获取下标(index)的方法

    原文:http://stackoverflow.com/questions/28012205/map-or-reduce-with-index-in-swiftYoucanuseenumeratetoconvertasequence(Array,String,etc.)toasequenceoftupleswithanintegercounterandandelementpairedtogethe

  10. Swift中的map 和 flatMap 原理及用法

    map和flatMap是Swift中两个常用的函数,它们体现了Swift中很多的特性。对于简单的使用来说,它们的接口并不复杂,但它们内部的机制还是非常值得研究的,能够帮助我们够好的理解Swift语言。map简介首先,咱们说说map函数如何使用。letnumbers=[1,2,3,4]letresult=numbers.map{$0+2}print//[3,4,5,6]map方法接受一个闭包作为参数,然后它会遍历整个numbers数组,并对数组中每一个元素执行闭包中定义的操作。比如咱们这个例子里面的闭包是讲

随机推荐

  1. js中‘!.’是什么意思

  2. Vue如何指定不编译的文件夹和favicon.ico

    这篇文章主要介绍了Vue如何指定不编译的文件夹和favicon.ico,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  3. 基于JavaScript编写一个图片转PDF转换器

    本文为大家介绍了一个简单的 JavaScript 项目,可以将图片转换为 PDF 文件。你可以从本地选择任何一张图片,只需点击一下即可将其转换为 PDF 文件,感兴趣的可以动手尝试一下

  4. jquery点赞功能实现代码 点个赞吧!

    点赞功能很多地方都会出现,如何实现爱心点赞功能,这篇文章主要为大家详细介绍了jquery点赞功能实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  5. AngularJs上传前预览图片的实例代码

    使用AngularJs进行开发,在项目中,经常会遇到上传图片后,需在一旁预览图片内容,怎么实现这样的功能呢?今天小编给大家分享AugularJs上传前预览图片的实现代码,需要的朋友参考下吧

  6. JavaScript面向对象编程入门教程

    这篇文章主要介绍了JavaScript面向对象编程的相关概念,例如类、对象、属性、方法等面向对象的术语,并以实例讲解各种术语的使用,非常好的一篇面向对象入门教程,其它语言也可以参考哦

  7. jQuery中的通配符选择器使用总结

    通配符在控制input标签时相当好用,这里简单进行了jQuery中的通配符选择器使用总结,需要的朋友可以参考下

  8. javascript 动态调整图片尺寸实现代码

    在自己的网站上更新文章时一个比较常见的问题是:文章插图太宽,使整个网页都变形了。如果对每个插图都先进行缩放再插入的话,太麻烦了。

  9. jquery ajaxfileupload异步上传插件

    这篇文章主要为大家详细介绍了jquery ajaxfileupload异步上传插件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. React学习之受控组件与数据共享实例分析

    这篇文章主要介绍了React学习之受控组件与数据共享,结合实例形式分析了React受控组件与组件间数据共享相关原理与使用技巧,需要的朋友可以参考下

返回
顶部