在 ng-switch 中使用 ng-transclude [英] Using ng-transclude inside ng-switch

查看:27
本文介绍了在 ng-switch 中使用 ng-transclude的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让 ng-transclude 在 ng-switch-default 指令中工作.这是我的代码:

指令:

.directive('field', ['$compile', function($complile) {返回 {限制:'E',范围: {ngModel: '=',类型: '@',},转置:真实,templateUrl: 'partials/formField.html',替换:真};}])

partials/formField.html

<input ng-switch-when="text" ng-model="$parent.ngModel" type="text"><div ng-switch-default><div ng-transclude></div>

我这样称呼它...

测试...</field>

产生错误的原因:

[ngTransclude:orphan] 在模板中非法使用 ngTransclude 指令!没有找到需要嵌入的父指令.

它可以顺利运行,在 ng-switch 指令之外,我不知道如何让它工作.有什么建议吗?

这是一个现场演示:http://plnkr.co/edit/3CEj5OY8uXMag75Xnliq?p=preview

解决方案

摘自:Github 问题

问题是 ng-switch 也使用了 transclude,这导致了错误.

在这种情况下,您应该创建一个使用正确的 $transclude 函数的新指令.为此,请将 $transclude 存储在您的父指令的控制器中(在您的 case 字段中),并创建一个引用该控制器并使用其 $transclude 函数的新指令.

在您的示例中:

.directive('field', function() {返回 {....控制器:['$transclude',函数($transclude){this.$transclude = $transclude;}],转置:真实,....};}).directive('fieldTransclude', function() {返回 {要求:'^字段',链接:函数($scope,$element,$attrs,fieldCtrl){fieldCtrl.$transclude(function(clone) {$element.empty();$element.append(clone);});}}})

在 html 中,您只需使用

而不是

.

这是一个更新的 plunker:http://plnkr.co/edit/au6pxVpGZz3vWTUcTCFT?p=预览

I'm having trouble getting ng-transclude to work within an ng-switch-default directive. Here's my code:

Directive:

.directive('field', ['$compile', function($complile) {
        return {
            restrict: 'E',
            scope: {
                ngModel: '=',
                type: '@',
            },
            transclude: true,
            templateUrl: 'partials/formField.html',
            replace: true
        };
    }])

partials/formField.html

<div ng-switch on="type">
    <input ng-switch-when="text" ng-model="$parent.ngModel" type="text">
    <div ng-switch-default>
        <div ng-transclude></div>
    </div>
</div>

I call it like so...

<field type="other" label="My field">
    test...
 </field>

Which produces the error:

[ngTransclude:orphan] Illegal use of ngTransclude directive in the template! No parent directive that requires a transclusion found.

It works without a hitch, outside of the ng-switch directive, I'm at a loss on how to get this working though. Any suggestions?

EDIT: Here's a live demo: http://plnkr.co/edit/3CEj5OY8uXMag75Xnliq?p=preview

解决方案

Taken from: Github issue

The problem is that ng-switch is using a transclude as well, which leads to the error.

In this case, you should create a new directive that uses the right $transclude function. For this to work, store the $transclude in the controller of your parent directive (in your case field), and create a new directive that references that controller and uses its $transclude function.

In your example:

.directive('field', function() {
  return {
       ....
      controller: ['$transclude', function($transclude) {
        this.$transclude = $transclude;
      }],
      transclude: true,
       ....
  };
})
.directive('fieldTransclude', function() {
  return {
    require: '^field',
    link: function($scope, $element, $attrs, fieldCtrl) {
      fieldCtrl.$transclude(function(clone) {
        $element.empty();
        $element.append(clone);
      });
    }
  }
})

In the html, you then just use <div field-transclude> instead of <div ng-transclude>.

Here is an updated plunker: http://plnkr.co/edit/au6pxVpGZz3vWTUcTCFT?p=preview

这篇关于在 ng-switch 中使用 ng-transclude的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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