Angular: Empty Fiddle

http://angularjs.org/

by IngoVals

HTML

<script src="http://code.angularjs.org/angular-1.0.1.js"></script>
<div ng-controller="MyCtrl">
    <p>X is {{myThing.x}}</p>
    <p>Y is {{myThing.y}}</p>
    <p>total: {{myThing.add()}}</p>
    <br/>Change X
    <input type="number" ng-model="myThing.x" />
    <br/>Change Y
    <input type="number" ng-model="myThing.y" />
    <br/>
    <p>{{newAdd()}}</p>
    <p>{{total}}</p>
</div>

JavaScript

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

//myApp.directive('myDirective', function() {});
myApp.factory('thing', function () {
    var thing = function (x, y) {
        this.x = x;
        this.y = y;
    }

    //computed property needed prototype keyword.
    thing.prototype.add = function () {
        return self.x + self.y;
    }

    return thing;
});

function MyCtrl($scope, thing) {
    $scope.myThing = new thing(1, 2);
    
    $scope.newAdd = function() {
        return $scope.myThing.x + $scope.myThing.y;
    }
    
    $scope.total = $scope.myThing.add();
}