1

This post originally asked the question, and includes a popular answer, but I can't find a way of updating the code to work with ControllerAs.

var app = angular.module('myApp', []);

app.filter('slice', function() {
  return function(arr, start, end) {
    return arr.slice(start, end);
  };
});

app.controller('MainController', function() {
  vm = this;
  vm.items = [];
  vm.start = 0;
  vm.end = 20;
  for (var i = 0; i < 100; i++) vm.items.push(i);
  return vm;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp">
  <div ng-controller='MainController as main'>
    Start: <input ng-model="main.start"> End: <input ng-model="main.end">
    <ul>
      <li ng-repeat="item in items | slice:main.start:main.end">{{item}}</li>
    </ul>
  </div>
</div>

1 Answer 1

5

checkout this plnkr

When you work with controllerAs syntax you need to use controller pointer to access its properties and methods.

<div ng-app="myApp">
  <div ng-controller='MainController as main'>
    Start: <input ng-model="main.start"> End: <input ng-model="main.end">
    <ul>
      <li ng-repeat="item in main.items | slice:main.start:main.end">{{item}}</li>
    </ul>
  </div>
</div>
</body>
Sign up to request clarification or add additional context in comments.

3 Comments

Good catch. The whole vm = this; thing though looks bad to me. It's an undeclared (and two letters!) variable to start with....
yeah.. people get confuse with $scope and controller as syntax. or sometime its just syntax problem..
Whoops - one too few main. Apologies for uninteresting question.

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.