您可以在Angular中转换为子指令吗?

沃尔特·魏德纳

我希望能够做这样的事情在我的应用程序:

<pill-autocomplete>
  <pill-template>{{item.name}}</pill-template>
</pill-autocomplete>

在哪里pill-autocomplete有一个模板可以转换成这样的子指令:

<pills ng-transclude="pillTemplate"></pills>
<input type="text">

这似乎并不可能因为NG-transclude创建范围和<pills>指令具有分离范围。

我想到的一种方法是将药丸模板注入自动完成的模板函数中。这样做的问题是它失去了包含范围。我还必须在与药丸具有相似行为的每个指令中都这样做。

还有其他方法可以在角度1.x中完成此操作吗?

塞尔吉奥

问题是,你的时间transclude从丸自动完成数据,你已经删除了里面的内容丸丸。

该transclusion替换所以在丸内容指令模板根本无法加载,因为已被transclusion覆盖指令模板下的内容。

我的建议很简单,不与NG-transclude中直接使用的标签,使用内部div来使尽可能指令来加载其内容

angular.module('app', []);
  var app = angular.module('app');

  'use strict';

  var app =  angular.module('app');

  app.controller('testController', [
    function () {
      var vm = this;
      vm.name = 'Jimmy';
    }]);

  app.directive('pillAutocomplete', function () {
    return {
      priority: 100,
      restrict: 'E',
      transclude: true,
      template: '<pills><p>From Pill-Autocomplete</p><div ng-transclude><div></pills>'
    };
  });

  app.directive('pills', function () {
    return {
      restrict: 'E',
      transclude: true,
      link: function (scope, element, attrs) {
        scope.style = true;
      },
      template: '<p>Inside Pills</p><div ng-class="{pillscolor : style}" ng-transclude></div>'
    };
  });
.pillscolor{
  color: green;
  font-size: 20px;
  font-weight: bold;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<article ng-app="app">
<section ng-controller="testController as test">
 Controller scope - {{test.name}}
  <pill-autocomplete>
     From controller - {{test.name}}
  </pill-autocomplete>
</section>
</article>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

您可以在Angular 2中使用独立指令吗?

我可以在Titanium中转换为Swift吗?

您可以将时间戳转换为FieldValue吗?

您可以将.sh文件转换为.bat文件吗?

您可以将VectorDrawable pathData转换为Path对象吗?

您可以将Stripe Java代码转换为CFscript吗

您可以在VS2015中转换* .json文件吗

在 C# 中,您可以将对象类型向上转换为通用“对象”的集合类型吗?

您可以在锈中将Some(box |:|)强制转换为Option <Box <FnOnce <... >>>吗?

您可以在不解压的情况下将表转换为vararg吗?

将List <Byte>转换为String,您可以帮助重构此(小型)方法吗?

您可以将UrlFetchApp转换为Apps脚本中的Promise吗?

您可以将此逻辑转换为其他形式吗?如果

您可以将JSON读入Laravel并转换为DOM元素吗?

您可以将Java Map <Long,CustomObject>转换为byte [],然后再转换回Map <Long,CustomObject>吗?

您可以将“异步关键字异步”功能转换为“非异步关键字异步”功能吗?

您可以使用自定义比较器将std :: map转换为无序映射吗?

将jQuery插件转换为指令Angular

将Fancybox插件转换为Angular指令

将angularjs指令转换为angular 10

我可以在Angular2的指令中访问子元素吗?

困惑:@Override无法在Java中转换为Annotation吗?

如何检查 JSON 是否可以在 Swift 中转换为字典?

您可以将“函数指针指向指针”转换为void *

可以在 VSTS 版本中转换 ELMAH 主题吗?

您可以在LPAD中使用子查询吗?

您可以在绘图中更改子图标题位置吗?

您可以为CSS样式添加可选的子元素吗?

OpenAPI:您可以在示例中使用子组件吗?