Building Pluggable Components in AngularJS: Before

by codef0rmer

HTML

<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.3/angular.min.js"></script>
<div ng-controller="OneCtrl">
    <button class="btn btn-primary home" ng-click="toggle('home')" ng-class="{true: 'btn-primary', false: 'btn-success'}[openPanel == 'home']">Home</button>
    <button class="btn btn-primary profile" ng-click="toggle('profile')" ng-class="{true: 'btn-primary', false: 'btn-success'}[openPanel == 'profile']">Profile</button>
</div>

<div ng-controller="HomeCtrl" class='home-template' ng-show="openPanel == 'home'">
    <ul>
        <li ng-repeat="choice in choices" ng-bind-template="{{$index + 1}}. {{choice}}"></li>
    </ul>
</div>

<div ng-controller="ProfileCtrl" class='profile-template' ng-show="openPanel == 'profile'">
    <ul>
        <li ng-repeat="choice in choices" ng-bind-template="{{$index + 1}}. {{choice}}"></li>
    </ul>
</div>

CSS

div[ng-controller="OneCtrl"] {
    background: gray;
    width: 100px;
    height: 100%;
    position: absolute;
    left: 0px;
    bottom: 0px;
}
.home {
    width: 80px;
    position: absolute;
    left: 10px;
    top: 10px;
}
.profile {
    width: 80px;
    position: absolute;
    left: 10px;
    top: 55px;
}
.home-template, .profile-template {
    position: absolute;
    background: rgb(40, 96, 117);
    left: 100px;
    top: 0px;
    bottom: 0px;
    right: 0px;
    padding: 20px;
    overflow: auto;
    color: white;
}
.profile-template { background: rgb(150, 164, 170); color: rgb(44, 43, 40);  }

JavaScript

var App = angular.module('App',[]);

App.run(function($rootScope) {
   $rootScope.openPanel = 'home'; 
});

App.controller("OneCtrl", function($scope, $rootScope) {
    $scope.toggle = function(panelName) {
        $rootScope.openPanel = $rootScope.openPanel === panelName ? '' : panelName;
        
        if (panelName === 'home') {
            $rootScope.$broadcast('HomeCtrl:open', {});
        } else {
            $rootScope.$broadcast('ProfileCtrl:open', {});   
        }
    };
});

App.controller('HomeCtrl', function($scope) {
   $scope.choices = [
       'Share whats new...',
       'You may know',
       'Updates from followers',
       'Few posts from communities',
       'Scrolling now for more updates',
       'Loading... :-)'
   ];
});

App.controller('ProfileCtrl', function($scope) {
   $scope.choices = [
       'Very large and funny cover Image',
       'Some important information such name, place, followers, etc',
       'Share whats new',
       'All the posts shared by me',
       'Scrolling now for more posts',
       'Loading... :-)'
   ];
});