Created
October 10, 2013 20:46
-
-
Save abusedmedia/6925387 to your computer and use it in GitHub Desktop.
Angularjs quick test on how to create directives and services as separate module, then how to use in your app.
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
// create directives | |
angular.module('whathever_namespaces', []).directive('myReusableComponent', function($parse){ | |
return { | |
link: function(scope, element, attrs){ | |
$(element).append('<h3>I\'m a reusable component</h3>'); | |
} | |
}; | |
}).directive('myReusableComponent2', function($parse){ | |
return { | |
link: function(scope, element, attrs){ | |
$(element).append('<h3>I\'m a reusable component 2</h3>'); | |
} | |
}; | |
}); | |
// create a directive | |
angular.module('whathever_namespaces3', []).directive('myReusableComponent3', function($parse){ | |
return { | |
link: function(scope, element, attrs){ | |
$(element).append('<h3>I\'m a reusable component 3</h3>'); | |
} | |
}; | |
}); | |
// create a service | |
angular.module('whathever_service', []) | |
.factory('myFactory', function(){ | |
return { | |
link: function(){ | |
return "HEY"; | |
} | |
}; | |
}); | |
//include it in your app | |
var app = angular.module('app', ['whathever_namespaces', 'whathever_namespaces3', 'whathever_service']); | |
function ctrl($scope, myFactory){ | |
console.log('ctrlc'); | |
var a = myFactory.link(); | |
console.log(a); | |
} | |
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
<!DOCTYPE html> | |
<html lang="en" ng-app="app"> | |
<head> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> | |
<meta name="description" content="Angularjs directive 101 - 01" /> | |
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script> | |
<meta charset="utf-8"> | |
<title></title> | |
</head> | |
<body ng-controller="ctrl"> | |
<div my-reusable-component></div> | |
<div my-reusable-component2></div> | |
<div my-reusable-component3></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment