Binding directive scope with controller in AngularJS

ste

I'm using a directive to check how much the user is scrolling the window but I can't manage to bind the scope with the controller.

Here's the directive and controller code:

'use strict';

angular.module('myApp.landing', ['ngRoute'])

.config(['$routeProvider', function($routeProvider) {
}])
.controller('landingCtrl', ["$scope", function($scope) {
    $scope.scrolled = false;

    $scope.$on('$routeChangeSuccess', function() {
    });
}]).
directive("scroll", ["$window", function($window){
    return {
        scope: false,
        link: function($scope, element, attrs){
            angular.element($window).bind("scroll", function(){
                if (this.pageYOffset >= 150) {
                     $scope.scrolled = true;
                     console.log($scope.scrolled + 'Scrolled 100px');
                 } else {
                     $scope.scrolled = false;
                     console.log($scope.scrolled + 'Not scrolled enough');
                 }
            });
        }
    };
}]);

this is the view code:

<div ng-controller="landingCtrl" scroll>
    <div class="row">
        <div class="col-sm-12 col-md-9 landing-square">{{ scrolled }}</div>
        <div class="col-sm-12 col-md-3 landing-square"></div>
        ....
</div>

In the view scrolled is not defined. If I define it in the controller I can see it, but the directive can't change its value. Basically I want in the view the variable "scrolled" that is changing value according to the directive.

What am I missing?

Jeroen

Because you're changing a something on the scope in a place Angular does not "know" about (e.g. a custom DOM event handler), you need to explicitly tell it to apply that change:

angular.element($window).bind("scroll", function(){
    if (this.pageYOffset >= 150) {
         $scope.$apply(function() { $scope.scrolled = true; });
         console.log($scope.scrolled + ' Scrolled 100px');
     } else {
         $scope.$apply(function() { $scope.scrolled = false; });
         console.log($scope.scrolled + ' Not scrolled enough');
     }
});

See this example, and see this excellent Q&A on $scope $apply and $watch. Or just go straight to the relevenat documentation (which is dry/technical, but explains the reasoning bhind it as well).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Directive - controller data binding in AngularJS

From Dev

AngularJS initializing $scope of directive/controller

From Dev

AngularJS directive: scope with named controller

From Dev

AngularJS Isolated Scope and Controller Binding

From Dev

Isolate scope directive with optional ampersand binding in angularjs?

From Dev

Angularjs two way binding between directive and controller

From Dev

AngularJS $scope Undefined When Compiling Controller In Directive

From Dev

Change controller scope value from directive AngularJS

From Dev

scope is undefined in AngularJS directive with ajax request in controller

From Dev

AngularJS controller as and this instead of $scope calling $apply in directive

From Dev

AngularJS $watch controller variable from a directive with scope

From Dev

angularjs directive + apply scope value in many controller

From Dev

angularjs controller access directive scope value

From Dev

AngularJS: directive adds object to $scope but undefined in controller

From Dev

AngularJS: Parent scope is not updated in directive (with isolated scope) two way binding

From Dev

AngularJS directive controller's scope accessible from outside directive, why?

From Dev

AngularJS: Proper scope binding when dynamically compiling a directive template

From Java

Use of symbols '@', '&', '=' and '>' in custom directive's scope binding: AngularJS

From Dev

AngularJS : directive binding value to parent scope from template model

From Dev

angularJS directive with isolated scope, attribute binding doesn't work

From Dev

AngularJS Directive Binding link: has no access to scope variables

From Dev

AngularJS : directive binding value to parent scope from template model

From Dev

AngularJS: Proper scope binding when dynamically compiling a directive template

From Dev

AngularJS Directive Binding link: has no access to scope variables

From Dev

Angularjs - Directive Two-way binding Isolated Scope Issue

From Dev

Scope binding within a directive

From Dev

How to pass a function handler from controller into directive isolated scope in AngularJs?

From Dev

AngularJS - Setting a controller's $scope variable in a custom directive that accesses the DOM

From Dev

AngularJS : Passing a function to an isolated scope of a directive to be called within its controller?

Related Related

  1. 1

    Directive - controller data binding in AngularJS

  2. 2

    AngularJS initializing $scope of directive/controller

  3. 3

    AngularJS directive: scope with named controller

  4. 4

    AngularJS Isolated Scope and Controller Binding

  5. 5

    Isolate scope directive with optional ampersand binding in angularjs?

  6. 6

    Angularjs two way binding between directive and controller

  7. 7

    AngularJS $scope Undefined When Compiling Controller In Directive

  8. 8

    Change controller scope value from directive AngularJS

  9. 9

    scope is undefined in AngularJS directive with ajax request in controller

  10. 10

    AngularJS controller as and this instead of $scope calling $apply in directive

  11. 11

    AngularJS $watch controller variable from a directive with scope

  12. 12

    angularjs directive + apply scope value in many controller

  13. 13

    angularjs controller access directive scope value

  14. 14

    AngularJS: directive adds object to $scope but undefined in controller

  15. 15

    AngularJS: Parent scope is not updated in directive (with isolated scope) two way binding

  16. 16

    AngularJS directive controller's scope accessible from outside directive, why?

  17. 17

    AngularJS: Proper scope binding when dynamically compiling a directive template

  18. 18

    Use of symbols '@', '&', '=' and '>' in custom directive's scope binding: AngularJS

  19. 19

    AngularJS : directive binding value to parent scope from template model

  20. 20

    angularJS directive with isolated scope, attribute binding doesn't work

  21. 21

    AngularJS Directive Binding link: has no access to scope variables

  22. 22

    AngularJS : directive binding value to parent scope from template model

  23. 23

    AngularJS: Proper scope binding when dynamically compiling a directive template

  24. 24

    AngularJS Directive Binding link: has no access to scope variables

  25. 25

    Angularjs - Directive Two-way binding Isolated Scope Issue

  26. 26

    Scope binding within a directive

  27. 27

    How to pass a function handler from controller into directive isolated scope in AngularJs?

  28. 28

    AngularJS - Setting a controller's $scope variable in a custom directive that accesses the DOM

  29. 29

    AngularJS : Passing a function to an isolated scope of a directive to be called within its controller?

HotTag

Archive