0

Here is a code I am working on for infinite scroll using angularJS. But the code is not working, also shows no error. Here is my code:

<!doctype html>
<html  ng-app="myApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
</head>

<style>
li {
  height: 120px;
  border-bottom: 1px solid gray;
}

#fixed {
    height: 400px;
    overflow: auto;
}

</style>

<body ng-controller="myCtrl">
<div id="fixed" when-scrolled="loadMore()" >
  <ul>
    <li ng-repeat="i in items">{{i.id}}</li>
  </ul>  
</div>

<script>
var app = angular.module('myApp', []);
myApp.controller('myCtrl', function($scope){

    $scope.items = [];
    
    var counter = 0;
    $scope.loadMore = function() {
        for (var i = 0; i < 5; i++) {
            $scope.items.push({id: counter});
            counter += 10;
        }
    };
    
    $scope.loadMore();
}

angular.module('scroll', []).directive('whenScrolled', function() {
    return function(scope, elm, attr) {
        var raw = elm[0];
        
        elm.bind('scroll', function() {
            if (raw.scrollTop + raw.offsetHeight >= raw.scrollHeight) {
                scope.$apply(attr.whenScrolled);
            }
        });
    };
});

</script>
</body>
</html>

I think there is some problem with the ng-app or ng-controller. As I am new to angularjs please help me with this.

Thanks in advance.

2
  • do you get an error in console? Commented Aug 13, 2015 at 6:05
  • Yes, Now I am getting an error. You can copy my code as it is or run the snippet to see the error. Thankx! Commented Aug 13, 2015 at 6:12

1 Answer 1

1

Besides the fact that you have lots of syntax mistakes I don't see any troubles with code. It works when the following is corrected.

Wrong variable reference:

var app = angular.module('myApp', []);//'app' variable declared but later 
//referenced as 'myApp'
app.controller('myCtrl', function($scope){

controller function missing ); in the end

And a new module declared for some reason for a directive and not injected into myApp. Unless you have strong reasons that you clearly understand, don't spawn unnecessary modules. Get the same one you already defined

app.directive('whenScrolled', function() {
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.