cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <html ng-app="vishalsrini">
<body ng-controller="MainCtrl as mainCtrl">
 <h1>Hello Controllers!</h1>
  <a href="https://codepen.io/vishal_srini/details/YGWqNJ/">Please have a look at this to know more about the main difference between controller and service</a><br/><br/>
 <button ng-click="mainCtrl.open('first')">
 Open First
 </button>
 <button ng-click="mainCtrl.open('second')">
 Open Second
 </button>
 <div ng-switch on="mainCtrl.tab">
 <div ng-switch-when="first">
 <div ng-controller="SubCtrl as ctrl">
 <h3>First tab</h3>
 <ul>
 <li ng-repeat="item in ctrl.list()">
 <span ng-bind="item.label"></span>
 </li>
 </ul>
 <button ng-click="ctrl.add()">
 Add More Items
 </button>
 </div>
 </div>
 <div ng-switch-when="second">
 <div ng-controller="SubCtrl as ctrl">
 <h3>Second tab</h3>
 <ul>
 <li ng-repeat="item in ctrl.list()">
 <span ng-bind="item.label"></span>
 </li>
 </ul>
 <button ng-click="ctrl.add()">
 Add More Items
 </button>
 </div>
 </div>
 </div>
<script
 src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.js">
</script>
<script src="app.js"></script>
</body>
</html>
            
          
!
            
              function ItemService(opt_items) {
 var items = opt_items || [];
 this.list = function() {
 return items;
 };
 this.add = function(item) {
 items.push(item);
 };
}
angular.module('vishalsrini', [])
 .provider('ItemService', function() {
 var haveDefaultItems = true;
 this.disableDefaultItems = function() {
 haveDefaultItems = false;
 };
 // This function gets our dependencies, not the
 // provider above
 this.$get = [function() {
 var optItems = [];
 if (haveDefaultItems) {
 optItems = [
 {id: 1, label: 'Item 0'},
 {id: 2, label: 'Item 1'}
 ];
 }
 return new ItemService(optItems);
 }];
 })
 .config(['ItemServiceProvider',
 function(ItemServiceProvider) {
 // To see how the provider can change
 // configuration, change the value of
 // shouldHaveDefaults to true and try
 // running the example
 var shouldHaveDefaults = false;
 // Get configuration from server
 // Set shouldHaveDefaults somehow
 // Assume it magically changes for now
 if (!shouldHaveDefaults) {
 ItemServiceProvider.disableDefaultItems();
 }
 }])
 .controller('MainCtrl', [function() {
 var self = this;
 self.tab = 'first';
 self.open = function(tab) {
 self.tab = tab;
 };
 }])
 .controller('SubCtrl',
 ['ItemService', function(ItemService) {
 var self = this;
 self.list = function() {
 return ItemService.list();
 };
 self.add = function() {
 ItemService.add({
 id: self.list().length + 1,
 label: 'Item ' + self.list().length
 });
 };
 }]);
            
          
!
999px
Loading ..................

Console