0

I am trying to toggle a class on an element if/when the page has scrolled.

I have followed this SO thread but when I try chaining in a directive, I don't even get to the log statements.

Trying to follow along with the angular docs, I've got my component.js file like this. I can get the log statements to show, but the class never gets toggled.

dashboard.component.js

angular.module('dashboard')
    .component('dashboard', {
    'templateUrl': 'dashboard/dashboard.template.html',
    controller: function dashboardController($scope, $window, $mdSidenav) {
        var primaryAppBar = angular.element(document.querySelector('#primaryAppBar'));

        angular.element($window).bind('scroll', function () {
            if (this.pageYOffset > 0) {
                console.log('add shadow');
                $scope.boolChangeClass = true;
            } else {
                console.log('remove shadow');
                $scope.boolChangeClass = false;
            }
        });
        ...

app-bar.template.html

<md-toolbar id="primaryAppBar" ng-class="{'md-whiteframe-1dp':boolChangeClass}">
...
3
  • 1
    Is the scope you're modifying in dashboard.component.js the same as in app-bar.template.html? I see nothing to indicate that it is. Commented Jan 5, 2017 at 17:56
  • @HarrisWeinstein It should be painfully obvious how much I have to learn. That was in fact the problem. Thank you so much! Commented Jan 5, 2017 at 18:29
  • Glad it worked! I'll add an answer so you can mark this as resolved. Commented Jan 5, 2017 at 19:54

1 Answer 1

0

The scope in dashboard.component.js is not the same scope as in app-bar.template.html. Either have them be under the same controller or provide something to pass the boolChangeClass variable.

Sign up to request clarification or add additional context in comments.

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.