Skip to content

Instantly share code, notes, and snippets.

@YurePereira
Last active February 3, 2017 14:04
Show Gist options
  • Select an option

  • Save YurePereira/dcc110f765cde55ce14d13f23b63fe8a to your computer and use it in GitHub Desktop.

Select an option

Save YurePereira/dcc110f765cde55ce14d13f23b63fe8a to your computer and use it in GitHub Desktop.
AngularJS - Using $rootScope.
<!doctype html>
<html>
<head>
<style type="text/css">
#app {
width: 200px;
padding: 20px;
}
#app .ctrl {
height: 50px;
margin: 10px;
}
.init {background-color: #ddd;}
.red {background-color: #f00;}
.blue {background-color: #00f;}
.green {background-color: #0f0;}
</style>
</head>
<body>
<div ng-app="app" ng-class="backgroundColor" id="app">
<div class="ctrl" ng-class="ctrlColor" ng-controller="HomeCtrl" ng-click="changeBackgroundColor('red')"></div>
<div class="ctrl" ng-class="ctrlColor" ng-controller="ContatoCtrl" ng-click="changeBackgroundColor(ctrlColor)"></div>
<div class="ctrl" ng-class="ctrlColor" ng-controller="SobreCtrl" ng-click="changeBackgroundColor(ctrlColor)"></div>
</div>
<script src="angular.js"></script>
<script src="script.js"></script>
</body>
</html>
var app = angular.module('app', []);
app.run(['$rootScope', function($rootScope) {
$rootScope.backgroundColor = 'init';
$rootScope.changeBackgroundColor = function(color) {
$rootScope.backgroundColor = color;
};
}]);
app.controller('HomeCtrl', ['$scope', '$rootScope', function($scope) {
$scope.ctrlColor = 'red';
}]);
app.controller('ContatoCtrl', ['$scope', '$rootScope', function($scope) {
$scope.ctrlColor = 'blue';
}]);
app.controller('SobreCtrl', ['$scope', '$rootScope', function($scope) {
$scope.ctrlColor = 'green';
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment