5

ng-switch-default ディレクティブ内で ng-transclude を動作させるのに問題があります。これが私のコードです:

指令:

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

パーシャル/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>

私はそう呼んでいます...

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

エラーが発生します:

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

ng-switch ディレクティブの外では、問題なく動作しますが、これを機能させる方法に途方に暮れています。助言がありますか?

編集: ここにライブ デモがあります: http://plnkr.co/edit/3CEj5OY8uXMag75Xnliq?p=preview

4

2 に答える 2

6

問題はng-switch、独自のトランスクルージョンを行っていることです。このため、あなたのトランスクルージョンは のトランスクルージョンで失われていng-switchます。

ここは使えないと思いますng-switch

ng-ifまたはng-show代わりに使用できます:

<input ng-if="type == 'text'" ng-model="$parent.ngModel" type="{{type}}" class="form-control" id="{{id}}" placeholder="{{placeholder}}" ng-required="required">
<div ng-if="type != 'text'">
    <div ng-transclude></div>
</div>
于 2013-11-15T11:32:23.897 に答える
0

出典: Github の問題

問題は、ng-switch がトランスクルードも使用しているため、エラーが発生することです。

この場合、正しい $transclude 関数を使用する新しいディレクティブを作成する必要があります。これを機能させるには、$transclude を親ディレクティブのコントローラー (ケース フィールド) に保存し、そのコントローラーを参照してその $transclude 関数を使用する新しいディレクティブを作成します。

あなたの例では:

.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);
      });
    }
  }
})

html では、<div field-transclude>代わりに<div ng-transclude>.

更新されたプランカーは次のとおりです: http://plnkr.co/edit/au6pxVpGZz3vWTUcTCFT?p=preview

于 2014-04-05T20:41:07.820 に答える