Skip to content

Instantly share code, notes, and snippets.

@AFelipeTrujillo
Last active March 7, 2016 18:02
Show Gist options
  • Save AFelipeTrujillo/0027be64836ac2a29799 to your computer and use it in GitHub Desktop.
Save AFelipeTrujillo/0027be64836ac2a29799 to your computer and use it in GitHub Desktop.
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;
});
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;
});
<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