Angular: Computed properties

http://angularjs.org/

by Alex

HTML

<script src="http://resources.holycrap.ws/jscripts/tiny_mce/tiny_mce.js"></script>
<script src="http://resources.holycrap.ws/jscripts/tiny_mce/jquery.tinymce.js"></script>
<script src="http://code.angularjs.org/angular-0.10.6.min.js"></script>
<div ng:controller="ComputedPropertiesCtrl" ng:app>
    <h1>before: </h1>
    {{data_before}}
    <h1>after: </h1>
    a <input ng:model="a"> + b <input ng:model="b"> = {{cp}}
</div>

CSS

h2 { position: fixed; right: 10px; top: 10px; color: red; background:white;z-index:1000; }
input { width: 30px; }

JavaScript

function ComputedPropertiesCtrl() {
    var scope = this;
    scope.a = 5;
    scope.b = 10;
    scope.data_before = [];
    scope.add = function() {
        return scope.a*1 + scope.b*1;
    };
    scope.$watch('[a, b]', function(currScope,newVal,oldVal) {
        scope.data_before = oldVal;
        scope.cp = scope.add();
    });
        
}