Skip to content

Instantly share code, notes, and snippets.

@ielcoro
Created May 24, 2016 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ielcoro/cd595265f0f8833a21ea226479600703 to your computer and use it in GitHub Desktop.
Save ielcoro/cd595265f0f8833a21ea226479600703 to your computer and use it in GitHub Desktop.
Angular JS
var hello = angular.module('hello', []);
hello.controller('helloController', ['$rootScope', '$scope', function ($rootScope, $scope) {
$scope.isLoggedIn = false;
$rootScope.$on('userLogin', function (e) {
$scope.isLoggedIn = true;
});
$rootScope.$on('userLogout', function (e) {
$scope.isLoggedIn = false;
});
}]);
<!doctype html>
<html ng-app="app" lang="en">
<head>
<meta charset="utf-8">
<title>Angular JS</title>
<script src="https://code.angularjs.org/1.5.5/angular.js"></script>
<link rel="stylesheet" href="styles.css">
<script src="loginController.js"></script>
<script src="helloController.js"></script>
<script src="script.js"></script>
</head>
<body>
<div ng-controller="helloController">
<h1 ng-show="isLoggedIn">Welcome! You are logged in!</h1>
<h1 ng-show="!isLoggedIn">You are NOT logged in! Please login</h1>
</div>
<div ng-controller="loginController">
<button ng-show="!loggedIn" ng-click="login()">Login</button>
<button ng-show="loggedIn" ng-click="logout()">Logout</button>
</div>
</body>
</html>
var login = angular.module('login', []);
login.controller('loginController', ['$rootScope', '$scope', function ($rootScope, $scope) {
$scope.loggedIn = false;
function login() {
console.log('Login!')
$scope.loggedIn = true;
$rootScope.$broadcast('userLogin');
}
function logout() {
console.log('Logout!')
$scope.loggedIn = false;
$rootScope.$broadcast('userLogout');
}
$scope.login = login;
$scope.logout = logout;
}]);
var app = angular.module('app', ['hello', 'login']);
/* todo: add styles */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment