Created
September 7, 2014 23:51
-
-
Save jeffling/75714a09024b649f8610 to your computer and use it in GitHub Desktop.
Experiment for computed properties in a Service // source http://jsbin.com/tayowe/2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html ng-app="demo"> | |
<head> | |
<meta name="description" content="Experiment for service computed values" /> | |
<title>Controller Messaging Demo</title> | |
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script> | |
</head> | |
<body> | |
<div data-ng-controller="TestCtrl"> | |
X: <input type="number" data-ng-model="TestService.x" /><br/> | |
Y: <input type="number" data-ng-model="TestService.y" /><br/> | |
Answer: {{TestService.answer}} | |
</div> | |
<script id="jsbin-javascript"> | |
var TestCtrl, TestService, app; | |
app = angular.module('demo', []); | |
TestService = (function() { | |
TestService.prototype.x = 1; | |
TestService.prototype.y = 2; | |
TestService.prototype.answer = 3; | |
function TestService($rootScope) { | |
$rootScope.$watch(((function(_this) { | |
return function() { | |
return _this.x + _this.y; | |
}; | |
})(this)), (function(_this) { | |
return function() { | |
console.log("Answer Changed", _this.x + _this.y); | |
return _this.answer = _this.x + _this.y; | |
}; | |
})(this)); | |
} | |
return TestService; | |
})(); | |
app.service('TestService', TestService); | |
TestCtrl = (function() { | |
function TestCtrl($scope, TestService) { | |
$scope.TestService = TestService; | |
} | |
return TestCtrl; | |
})(); | |
app.controller('TestCtrl', TestCtrl); | |
</script> | |
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html> | |
<html ng-app="demo"> | |
<head> | |
<meta name="description" content="Experiment for service computed values" /> | |
<title>Controller Messaging Demo</title> | |
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"><\/script> | |
</head> | |
<body> | |
<div data-ng-controller="TestCtrl"> | |
X: <input type="number" data-ng-model="TestService.x" /><br/> | |
Y: <input type="number" data-ng-model="TestService.y" /><br/> | |
Answer: {{TestService.answer}} | |
</div> | |
</body> | |
</html></script> | |
<script id="jsbin-source-javascript" type="text/javascript">app = angular.module 'demo', [] | |
class TestService | |
x: 1 | |
y: 2 | |
answer: 3 | |
constructor: ($rootScope) -> | |
$rootScope.$watch (=> @x + @y), => | |
console.log "Answer Changed", @x + @y | |
@answer = @x + @y | |
app.service 'TestService', TestService | |
class TestCtrl | |
constructor: ($scope, TestService) -> | |
$scope.TestService = TestService | |
app.controller 'TestCtrl', TestCtrl | |
</script></body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var TestCtrl, TestService, app; | |
app = angular.module('demo', []); | |
TestService = (function() { | |
TestService.prototype.x = 1; | |
TestService.prototype.y = 2; | |
TestService.prototype.answer = 3; | |
function TestService($rootScope) { | |
$rootScope.$watch(((function(_this) { | |
return function() { | |
return _this.x + _this.y; | |
}; | |
})(this)), (function(_this) { | |
return function() { | |
console.log("Answer Changed", _this.x + _this.y); | |
return _this.answer = _this.x + _this.y; | |
}; | |
})(this)); | |
} | |
return TestService; | |
})(); | |
app.service('TestService', TestService); | |
TestCtrl = (function() { | |
function TestCtrl($scope, TestService) { | |
$scope.TestService = TestService; | |
} | |
return TestCtrl; | |
})(); | |
app.controller('TestCtrl', TestCtrl); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment