Skip to content

Instantly share code, notes, and snippets.

@aaronfrost
Last active August 29, 2015 14:05
Show Gist options
  • Save aaronfrost/e40e9aaac38778885423 to your computer and use it in GitHub Desktop.
Save aaronfrost/e40e9aaac38778885423 to your computer and use it in GitHub Desktop.
Multiple Scopes - JS
<!--Parent Controller-->
<div ng-controller="ParentController">
<!--First Child Controller-->
<div ng-controller="ChildAController">
<button ng-click="handleClick()">Click This</button>
</div>
<!--Second Child Controller-->
<div ng-controller="ChildBController">
<button ng-click="handleClick()">Click This</button>
</div>
</div>
var app = angular.module("app", []);
app.controller('ParentController', function($scope){
});
app.controller('ChildAController', function($scope, fooService){
$scope.handleClick = function(e){
doControllerSpecificThings();
fooService.doAGenericThing();
};
function doControllerSpecificThings(){
//things that are for this controller here
}
});
app.controller('ChildBController', function($scope, fooService){
$scope.handleClick = function(e){
doControllerSpecificThings();
fooService.doAGenericThing();
};
function doControllerSpecificThings(){
//things that are for this controller here
}
});
app.factory('fooService', function(){
return {
doAGenericThing : function(){
console.log("Don't cry anymore. This is how you share code.");
}
};
});
@aaronfrost
Copy link
Author

Don't do this. Use a service.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment