2017-02-14 2 views
0

編集:詳細も追加しました。別の指令内の単純な角度指令 - コントローラの有効範囲にどのように値を渡すか

私の指示で選択した値をコントローラ$scopeに渡すことで助けてもらえます。

私は非常に基本的な指令を別の指令の中に置いていますが、その値はコントローラのスコープに渡されません。私は私のパラメータのための "未定義"の値を取得します。

しかし、小さなディレクティブは、他のディレクティブのタグ内ではなく、ビューのHTMLのどこにでも置くと機能します。

これが私の新しいディレクティブです:

(function() { 
    'use strict'; 

    angular.module('portalDashboardApp') 
     .directive('ogItemsPerPage', function() { 
      return { 
       restrict: 'E', 
       replace: true, 
       templateUrl: './tpls/ItemsPerPageTemplate.html', 
       scope: { 
        perPageCountOptions: [30, 50, 100, "ALL"], 
        selectedItemsPerPage: '@' 
       } 
      }; 
     }); 
})(); 

これはテンプレートです:

<div> 
    <div id="DropDownBox"> 
     <label for="ItemsPerpage">Items per Page: </label> 
     <select ng-change="changePageCount()" ng-model="selectedItemsPerPage" id="ItemsPerpage" ng-options="perPage for perPage in perPageCountOptions"></select> 
    </div> 
</div> 

これは私のコントローラに呼び出される関数です:

$scope.changePageCount = function() { 

    if ($scope.selectedItemsPerPage === "ALL") { 
     $scope.perPageCount = -1; 
    } 
    else { 
     $scope.perPageCount = $scope.selectedItemsPerPage; 
    } 
    pullSocialData(); 
} 

これは私が別の指令のタグ内に、私の<og-items-per-page>ディレクティブを配置しています図である。

<og-data-box heading="Tweet List" link="" uid="socialMentionsMeta" description=""> 
    <div class="dataStatus"> 
     {{dataStatus}} 
     <og-loading-indicator></og-loading-indicator> 
    </div> 
    <og-items-per-page></og-items-per-page> 
    <div class="dataContent" ng-show="dataContent" ng-mouseover="showGraphTrainingInfo()"> 
     <og-social-media-mentions-list></og-social-media-mentions-list> 

     <div ng-show="showMorePostLoading" id="morePostLoadingContainer"><div id="morePostLoadingInner"></div></div> 
    </div> 
</og-data-box> 

データボックスディレクティブ:

(function() { 
    'use strict'; 

    angular.module('portalDashboardApp') 
     .directive('ogDataBox', function() { 
      return { 
       restrict: 'E', 
       transclude: true, 
       replace: true, 
       scope: { 
        heading: '@', 
        link: '@', 
        uid: '@', 
        description: '@', 
        chartConfig: '@' 
       }, 
       link: function (scope) { 

        scope.boxOpenCloseTitle = 'Collapse'; 
        scope.iconStatus = 'upIcon'; 
        scope.contentStatus = ''; 
        var openCloseStatus = true; 
        var maximumSize = false; 
        scope.dataBoxUnderlayClass = ''; 
        scope.dataBoxMaxMinClass = ''; 
        scope.maxMinIcon = 'maximise'; 

        scope.openCloseDataBox = function() { 
         if (openCloseStatus) { 
          scope.boxOpenCloseTitle = 'Expand'; 
          openCloseStatus = false; 
          scope.iconStatus = 'downIcon'; 
          scope.contentStatus = 'hideContent'; 
         } 
         else { 
          scope.boxOpenCloseTitle = 'Collapse'; 
          openCloseStatus = true; 
          scope.iconStatus = 'upIcon'; 
          scope.contentStatus = ''; 
         } 
        }; 

        scope.maxMinDatabox = function() { 

         maximumSize = !maximumSize; 

         if (maximumSize) { 
          scope.dataBoxUnderlayClass = 'dataBoxUnderlayFullScreen'; 
          scope.dataBoxMaxMinClass = 'dataBoxMaximised'; 
          scope.maxMinIcon = 'minimise'; 
         } 
         else { 
          scope.dataBoxUnderlayClass = ''; 
          scope.dataBoxMaxMinClass = ''; 
          scope.maxMinIcon = 'maximise'; 
         } 

        }; 

       }, 
       templateUrl: './tpls/DataBoxTemplate.html' 
      }; 
     }); 
})(); 

データボックステンプレート:

<div ng-class="dataBoxUnderlayClass"> 
    <section class="dataBox" id="{{uid}}" ng-class="dataBoxMaxMinClass"> 
     <header class="dataBoxHeader"> 
      {{heading}} 
      <img src="images/openCloseIcon.svg" title="{{boxOpenCloseTitle}}" width="15" height="15" class="openCloseBox {{iconStatus}}" ng-click="openCloseDataBox()" /> 
      <img ng-mouseover="infoIconStyle='dataBoxInfoContentShow'" ng-mouseleave="infoIconStyle='dataBoxInfoContentHide'" src="images/info-icon.svg" height="15" class="dataBoxInfo" /> 
     </header> 
     <div class="dataBoxContent {{contentStatus}}"> 
      <div ng-class="infoIconStyle" class="dataBoxInfoContent">{{description}}</div> 
      <div ng-transclude></div> 
     </div> 
    </section> 
</div> 

必要に応じて他のディレクティブ内でディレクティブをネストできるように、変更する必要があるのは何ですか?

ありがとうございました!

+0

'og-data-box'定義を追加できますか? – Tako

+0

更新を掲載しました。ありがとうございました! – onmyway

+0

下記の私のコメントを参照 – Tako

答えて

0

この問題は、ディレクティブによって作成された分離スコープから発生します。

og-data-boxの中のhtmlは指令スコープの下にあり、コントローラスコープにアクセスできません。

http://angular-tips.com/blog/2014/03/transclusion-and-scopes/<og-data-box></og-data-box>のコントローラスコープにアクセスする場合は、隔離されたスコープに追加する必要があります。これはそれを行うための最善の方法ではありませんコメントで述べたように :そのような

何かがEDIT問題

app.directive('og-data-box', function() { 
    return { 
    restrict: 'EA', 
    scope: { 
     heading: '=', 
     link: '=', 
     uid: '=', 
     description: '=' 
    }, 
    transclude:true, 
    link: function(scope, element, attrs, ctrl, transclude) { 
     transclude(scope.$parent, function(clone, scope) { 
     element.append(clone); 
     }); 
    } 
    }; 
}); 

を修正する必要があります。

OG-データボックス(私はスコープにselectedItemsPerPageを追加)

(function() { 
'use strict'; 

angular.module('portalDashboardApp') 
    .directive('ogDataBox', function() { 
     return { 
      restrict: 'E', 
      transclude: true, 
      replace: true, 
      scope: { 
       heading: '@', 
       link: '@', 
       uid: '@', 
       description: '@', 
       chartConfig: '@', 
       selectedItemsPerPage: '@' 
      }, 
      link: function (scope) { 

       scope.boxOpenCloseTitle = 'Collapse'; 
       scope.iconStatus = 'upIcon'; 
       scope.contentStatus = ''; 
       var openCloseStatus = true; 
       var maximumSize = false; 
       scope.dataBoxUnderlayClass = ''; 
       scope.dataBoxMaxMinClass = ''; 
       scope.maxMinIcon = 'maximise'; 

       scope.openCloseDataBox = function() { 
        if (openCloseStatus) { 
         scope.boxOpenCloseTitle = 'Expand'; 
         openCloseStatus = false; 
         scope.iconStatus = 'downIcon'; 
         scope.contentStatus = 'hideContent'; 
        } 
        else { 
         scope.boxOpenCloseTitle = 'Collapse'; 
         openCloseStatus = true; 
         scope.iconStatus = 'upIcon'; 
         scope.contentStatus = ''; 
        } 
       }; 

       scope.maxMinDatabox = function() { 

        maximumSize = !maximumSize; 

        if (maximumSize) { 
         scope.dataBoxUnderlayClass = 'dataBoxUnderlayFullScreen'; 
         scope.dataBoxMaxMinClass = 'dataBoxMaximised'; 
         scope.maxMinIcon = 'minimise'; 
        } 
        else { 
         scope.dataBoxUnderlayClass = ''; 
         scope.dataBoxMaxMinClass = ''; 
         scope.maxMinIcon = 'maximise'; 
        } 

       }; 

      }, 
      templateUrl: './tpls/DataBoxTemplate.html' 
     }; 
    }); 
})(); 

あなたはog-data-boxの範囲に該当するスコープを渡す必要があり、あなたは、ネストされたディレクティブでそれを使用することができます

<og-data-box heading="Tweet List" link="" uid="socialMentionsMeta" description=""  selected-items-per-page="selectedItemsPerPage"> 
    <div class="dataStatus"> 
     {{dataStatus}} 
     <og-loading-indicator></og-loading-indicator> 
    </div> 
    <og-items-per-page selected-items-per-page="selectedItemsPerPage"></og-items-per-page> 
    <div class="dataContent" ng-show="dataContent" ng-mouseover="showGraphTrainingInfo()"> 
     <og-social-media-mentions-list></og-social-media-mentions-list> 

     <div ng-show="showMorePostLoading" id="morePostLoadingContainer"><div id="morePostLoadingInner"></div></div> 
    </div> 
</og-data-box> 
+0

応答ありがとう。私が問題を抱えているディレクティブは ''です。そのディレクティブからコントローラスコープに値を渡したい場合を除いて、すべてが完全に機能します。 – onmyway

+0

この投稿によると、 "$ parentを呼び出すと、分離されたスコープ(&)のポイントに違反するので、$ parentを呼び出すのは悪い練習です。" http://stackoverflow.com/questions/19667275/nested-directives-cannot-pass-args-to-control-method-from-child-directive-i – onmyway

+0

はい、それを行う最良の方法ではない、私は同意するあなたが言及した投稿 – Tako

関連する問題