AngularJS $digest by feature: the impossible way

by Xavier Boubert

HTML

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js"></script>
<div ng-app="app">
    
    <h1>AngularJS $digest by feature: the impossible way</h1>
    <p>
        In big projects, we have many features with many parts of view.<br />
        <span class="keyword">$apply</span> start refreshing the entire page by calling all of the dirty-checking algorythms and then update the DOM elements. Fine, DOM is not modified if it's unnecessary. But calling all of these algos can take lots of time.
    </p>
    <p>
        Instead of calling <span class="keyword">$apply</span> everytime, it's possible to use <span class="keyword">$digest</span> to refresh a specific <span class="keyword">$scope</span>. Ok so it's possible to call just the algos we wants for specific features instead of the big page.
    </p>
    <h2>The first problem:</h2>
    <p>
        All of the AngularJS behaviors are based on calling <span class="keyword">$apply</span>, when the user click buttons, with XHR requests, etc. Unless you want to re-code all of these features, $apply will be called many many times.<br />
        In my point of view, the developer should have the control of which features he want to <span class="keyword">$digest</span> instead of the whole page. It's the basic of a complicated UI with lots of components. Actually it's not possible to have a big project in a single page with many features with AngularJS. This is problematic because AngularJS is designed for this use. Try using Drag&amp;Drop with many components, it's the end of the world.
    </p>
    <h2>My simple example:</h2>
    <p>
        We have 3 features. We just want to <span class="keyword">$digest</span> "Feature A" and "Feature C" when user interacts with "Feature A". With a simple ng-click it's not possible because it calls <span class="keyword">$apply</span> that calls <span class="keyword">$digest</span> of "Feature...

CSS

body {
    font-family: helvetica, verdana, arial;
    font-size: 16px;
    line-height: 22px;
}

h1 {
    font-weight: bold;
    font-size: 34px;
    line-height: 40px;
}

h2 {
    font-weight: normal;
    padding: 0;
    font-size: 22px;
    line-height: 24px;
    margin: 30px 0 15px;
}

p {
    margin: 0 0 15px 0;
    padding: 0;
}

.clear {
    clear: both;
}

.keyword {
    font-style: italic;
}

.feature-a, .feature-b, .feature-c {
    color: white;
    float: left;
    width: 150px;
    height: 150px;
}

.feature-a {
    background: #2E8DEF;
}

.feature-b {
    background: #BF1E4B;
}

.feature-c {
    background: #00A600;
}

.feature-a p, .feature-b p, .feature-c p {
    padding: 10px;
}

.feature-a h3, .feature-b h3, .feature-c h3 {
    font-weight: normal;
    padding: 10px;
    font-size: 18px;
    line-height: 20px;
    margin: 0;
}

.feature-a input[type="text"] {
    width: 120px;
}

JavaScript

angular.module('app', ['ng'])

    .factory('featuresService', [function() {
        
        return new (function() {
            
            // first example
            
            var _fn = null;
                        
            this.refreshC = function() {
                if(_fn) _fn();
            };
            
            this.onRefreshC = function(fn) {
                if(!_fn) _fn = fn;
            };
            
            // second example
            
            var _valueFn = null
            
            this.changeValueC = function(newValue) {
                if(_valueFn) _valueFn(newValue);
            };
            
            this.onValueChangedC = function(fn) {
                _valueFn = fn;
            };
        })();
    }])

    .directive('featureA', function () {
        return {
            scope: true,
            controller: ['featuresService', '$scope', '$element', function (featuresService, $scope, $element) {
                var _times = 0;
                
                $scope.value = 'My Value';
                
                $scope.$watch(function() {
                    _times++;
                    $element.find('span').html(_times);
                });
                
                $scope.$watch('value', function(newValue) {
                    featuresService.changeValueC(newValue);
                });
                
                $element.find('.jquery-click').click(function() {
                    $scope.$digest();
                    featuresService.refreshC();
                });
                
                $element.find('.value').on('input', function(value) {
                    $scope.value = $(this).val();
                    $scope.$digest();
                });
                
                $scope.updateValue = function() { };
            
            }]
        };
    })

    .directive('featureB', function () {
        return {
            scope: true,
            controller:...