【问题标题】:Call angular UI Bootstrap directive from inside custom directive从自定义指令内部调用 Angular UI Bootstrap 指令
【发布时间】:2017-06-29 23:13:42
【问题描述】:

目前我正在处理一个非常广泛的表单,并在 HTML 上使用输入、文本区域、日期选择器等,这会使代码看起来非常难看,也很难阅读。 问题是我创建了返回正确 HTML 元素的自定义指令,例如:

在 HTML 中

<suggest-input data-ng-model="EDCtrl.headerStep.provider.identification"
               placeholder="'Ej. 888888-8'"
               label="'Identificador de emisor'">
</suggest-input>

指令:

var suggestInput = function ($compile, $http) {
  return {
     restrict: 'E',
     require: 'ngModel',
     templateUrl: templates + '/suggestInputTemplate.tpl.html',
     replace: true,
     scope: {
         model: '=ngModel',
         label: '=label',
         title: '=title',
         placeholder : '=placeholder'
     },
   };
};

模板

<div>
  <label>{{ label }}</label>
  <input class="form-control" data-ng-model="model" placeholder="{{ placeholder }}" call="functionName()"/>
</div>

我在自定义指令中使用角度引导指令时遇到问题,例如: 如何在自定义指令中使用这种配置调用“uib-typeahead”?

有什么想法吗?

【问题讨论】:

标签: javascript angularjs directive angular-bootstrap


【解决方案1】:

您可以在自己的指令中使用任何嵌套指令,angular-ui-boostrap 指令在这种情况下并不特殊。关于uib-typeahead,您可以在angular-ui-bootstrap 网站上看到以下示例:

<input type="text" ng-model="asyncSelected" 
   placeholder="Locations loaded via $http" 
   uib-typeahead="address for address in getLocation($viewValue)" 
   typeahead-loading="loadingLocations" 
   typeahead-no-results="noResults" class="form-control">

唯一需要知道的是ngModel 是指令本身,您可以通过link(scope, element, attrs,ngModelController) 访问它。 ngModelController 具有 $viewValue$modelValue 属性,它们表示来自外部范围的绑定值。所以而不是 scope:{model:'=ngModel'} 在指令中使用这些变量进行绑定。

【讨论】:

  • 对此很抱歉,但我没有看到您在评论中告诉我的内容,您能否提供一个 angular 1.6 的示例?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 2017-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多