Last active
February 3, 2017 14:04
-
-
Save YurePereira/dcc110f765cde55ce14d13f23b63fe8a to your computer and use it in GitHub Desktop.
AngularJS - Using $rootScope.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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