2016-08-14 18 views
0

に配列を利用できるようにする:AngularJSディレクティブ - 私は属性としての私のディレクティブ<strong>asScrollTop</strong>このHTMLフラグメント持つディレクティブ

<div data-as-scroll-top> 
    <div data-ng-repeat="chatMessageOfUser in vm.chatMessagesOfUser"> 
    <!-- use chatMessageOfUser --> 
    </div> 
</div> 

と、これは私のディレクティブです:今

(function() { 
'use strict'; 

angular 
    .module('myProject.common') 
    .directive('asScrollTop', asScrollTop); 

function asScrollTop(validateService) { 
    var directive = { 
     restrict: 'A', 
     link: link 
    }; 
    return directive; 

    //////////// 

    function link(scope, element, attr) { 
     console.log(element); 
     element.on('scroll', function() { 
      if(element[0].scrollTop <= 0) { 
       // here I need vm.chatMessagesOfUser or the first entry of the 
       // vm.chatMessagesOfUser array    
    } 
     }); 
     } 
} 
})(); 

私の質問をvm.chatMessagesOfUserアレイをディレクティブで使用できるようにする方法はありますか?

答えて

1

あなたの属性のディレクティブは、既存のスコープを使用しているので、変数は、そのスコープにある

var directive = { 
     restrict: 'A', 
     scope: { yourList: '=yourList' }, 
     link: link 
    }; 
    return directive; 

function link(scope, element, attr) { 
    console.log(scope.yourList); 
    }; 
HTMLマークアップで

と設定値

<div data-as-scroll-top your-list="vm.chatMessagesOfUser"></div> 
0

のようなディレクティブで有効範囲を定義したことができます。

function link(scope, element, attr) { 
    console.log(element); 
    element.on('scroll', function() { 
     if(element[0].scrollTop <= 0) { 
      // here I need vm.chatMessagesOfUser 
      console.log(scope.vm.chatMessaagesOfUser); 
      // or the first entry of the 
      // vm.chatMessagesOfUser array 
      console.log(scope.vm.chatMessangerOfUser[0]);    
     } 
    }); 
} 
関連する問題