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
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> |