javascript - 从自定义指令内部调用 Angular UI Bootstrap 指令

标签 javascript angularjs directive angular-bootstrap

目前我正在开发一个非常广泛的表单,并在 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>

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

有什么想法吗?

最佳答案

您可以在自己的指令中使用任何嵌套指令,并且 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 属性,它们表示来自外部范围的绑定(bind)值。所以而不是 scope:{model:'=ngModel'} 将这些变量用于指令内的绑定(bind)。

关于javascript - 从自定义指令内部调用 Angular UI Bootstrap 指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42179140/

相关文章:

javascript - 单击外部 Angular 分量

php - 检查用户名是否可用于 AJAX

javascript - 上传前 Dropzone JS 更改文件名

javascript - 检查 AngularJS 中自定义指令的输入

python - 您将如何在 Python 中执行相当于预处理器指令的操作?

javascript - 计算所选复选框分数的最大值和平均值

javascript - 单击文本时在转发器中选择/取消选择复选框

javascript - 在 Angularjs 中循环 ng-options

javascript - 无法插入将 $scope 变量传递到 Angular 过滤器时出现的错误

compilation - gfortran:非法预处理器指令和无效字符/非数字字符编译错误