Skip to content

Instantly share code, notes, and snippets.

@daviesgeek
Last active August 29, 2015 14:26
Show Gist options
  • Select an option

  • Save daviesgeek/7aeb31f75c0819c40e0e to your computer and use it in GitHub Desktop.

Select an option

Save daviesgeek/7aeb31f75c0819c40e0e to your computer and use it in GitHub Desktop.
Angular: load controller from another controller

Simple example of loading a controller from another controller in Angular. Note that this returns a new instance of the controller.

Running example: http://plnkr.co/edit/EjzYj0

angular.module('app', [])
.controller('One', function($scope) {
console.log('this is inside One!')
$scope.name = "Matthew"
})
.controller('Two', function($scope, $controller) {
console.log('this is inside Two!')
$controller('One', {'$scope':$scope})
})
<!DOCTYPE html>
<html ng-app="app">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body>
<div ng-controller="One">{{name}}</div>
<div ng-controller="Two">{{name}}</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment