AngularJS Services: Constants

by jhoguet

HTML

<div ng-app="app" ng-controller="MainCtrl as main">
    <h1 ng-repeat="item in main.items | filter:main.getSearch()">{{item}}</h1>
</div>
<ul></ul>

JavaScript

(function(ng, app){
    app = angular.module('app', []);
    
    app.controller('MainCtrl', function MainCtrl($timeout) {
            this.items = ['one', 'two'];
            
            this.getSearch = function(){
                console.log('searching');
                return 't';
            }
            
            $timeout(function(){
             // unrelated changes causes it to filter through the entire array again... it is happening on every watch!
            }, 1000);
        }
    );
    
}(angular));