Skip to content

Instantly share code, notes, and snippets.

@codef0rmer
Created October 15, 2012 05:45
Show Gist options
  • Save codef0rmer/3890976 to your computer and use it in GitHub Desktop.
Save codef0rmer/3890976 to your computer and use it in GitHub Desktop.
Sharing data between a controller and an application in AngularJS
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.0.1/angular.min.js"></script>
<script>
var myApp = angular.module('myApp', []);
myApp.run(function($rootScope) {
$rootScope.btnVal = 'You can change my value';
});
myApp.controller('myCtrl', function($scope, $rootScope) {
$scope.changeValue = function() {
$rootScope.btnVal = $scope.input || 'You can change my value';
};
});
</script>
<style>
div[ng-controller],html[ng-app] { border: 1px dashed red;}
</style>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<div>
<div ng-controller="myCtrl">
Change Value <input ng-model='input' ng-change="changeValue()" />
</div>
<button>{{btnVal}}</button>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment