Apply service to scope 1.0rc3
http://angularjs.org/
HTML
<script src="http://code.angularjs.org/angular-1.0.0rc3.js"></script>
<div ng-controller="SomeController">
Hello, {{name}}!
<br/>
<button ng-click="test()">Test</button>
<button ng-click="handleClickImpliedScope()">Direct call to service method with implied scope</button>
<button ng-click="handleClickExplicitScope($scope)">Direct call to service method with explicit scope</button>
</div>
JavaScript
var myApp = angular.module('myApp',[]);
myApp.factory('commonCode', function() {
return {
handleClickImpliedScope: function(){
this.name= 'clicked from common code implied scope';
},
handleClickExplicitScope: function(scope){
scope.name= 'clicked from common code explicit scope';
console.log ("name is " +this.name)
}
};
});
function SomeController($scope, commonCode) {
$scope.name = 'Superhero Child';
angular.extend($scope, commonCode);
$scope.test = function(){
commonCode.handleClickExplicitScope($scope);
}
}