0

I have this html fragment with my directive asScrollTop as attribute:

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

and this is my directive:

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

My question now would be how I can make the the vm.chatMessagesOfUser array available in directive?

2 Answers 2

1

You can defined scope in directive like

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

function link(scope, element, attr) {
    console.log(scope.yourList);
    };

and set value in html markup

<div data-as-scroll-top your-list="vm.chatMessagesOfUser"></div>
Sign up to request clarification or add additional context in comments.

Comments

0

Since your attribute directive uses the existing scope, the variables are on that scope.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.