Last active
March 7, 2016 18:02
-
-
Save AFelipeTrujillo/0027be64836ac2a29799 to your computer and use it in GitHub Desktop.
This file contains 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
mainApp.controller('groceriesStoreController', function($scope) { | |
$scope.sugar = {}; | |
$scope.sugar.name = "Sugar"; | |
$scope.sugar.stock = 10; | |
$scope.pasta = {}; | |
$scope.pasta.name = "Pasta"; | |
$scope.pasta.stock = 35; | |
}); |
This file contains 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 mainApp = angular.module("groceriesStoreApp", []); | |
mainApp.directive('item', function() { | |
var directive = {}; | |
//restrict = E, signifies that directive is Element directive | |
directive.restrict = 'E'; | |
//template replaces the complete element with its text. | |
directive.template = "Item: <b>{{item.name}}</b> , Roll No: <b>{{item.stock}}</b>"; | |
//scope is used to distinguish each student element based on criteria. | |
directive.scope = { | |
item : "=name" | |
} | |
//compile is called during application initialization. AngularJS calls it once when html page is loaded. | |
directive.compile = function(element, attributes) { | |
element.css("border", "1px solid #cccccc"); | |
//linkFunction is linked with each element with scope to get the element specific data. | |
var linkFunction = function($scope, element, attributes) { | |
element.html("Item: <b>"+$scope.item.name +"</b> , Stock: <b>"+$scope.item.stock+"</b><br/>"); | |
element.css("background-color", "#ff00ff"); | |
} | |
return linkFunction; | |
} | |
return directive; | |
}); |
This file contains 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
<divng-app="groceriesStoreApp" ng-controller="groceriesStoreController"> | |
<item name="sugar"></item> | |
<br /> | |
<item name="pasta"></item> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment