Skip to content

Instantly share code, notes, and snippets.

@rishabhgupta
Last active April 18, 2018 19:15
Show Gist options
  • Save rishabhgupta/d07fcd24cb4a9e5df7a7081d808ba618 to your computer and use it in GitHub Desktop.
Save rishabhgupta/d07fcd24cb4a9e5df7a7081d808ba618 to your computer and use it in GitHub Desktop.
Directives in Angular JS
myapp.directive("searchResult",function(){
return {
restrict: "AECM",
temlateUrl: "directives/searchResult.tpl.html",
replace: true,
scope: {
studentName: "@", // Symbol for creating a text hole
studentObject: "=", // Symbol for object hole
studentFunction: "&", // Symbol for function hole
},
compile: function(elem, attrs){
console.log(elem);
return {
post: function(scope, elements, attrs){
if (scope.studentObject.name === ""){
elements.removeAttr('class');
}
};
};
}
};
});
-->directives
-->searchResult
-->searchResult.directive.js
-->searchResult.tpl.html
<h1>Results</h1>
<searchResults></searchResults>
<searchResults></searchResults>
<searchResults></searchResults>
myapp.directive("searchResult",function(){
return {
restrict: "AECM",
temlateUrl: "directives/searchResult.tpl.html",
replace: true,
scope: {
// This is now the model for our directive
}
};
});
myapp.directive("searchResult",function(){
return {
temlate: '<div><h3>name</h3><p>Description</p><p>Address</p></div>',
replace: true
};
});
myapp.directive("searchResult",function(){
return {
restrict: 'AECM',
temlate: '<div><h3>name</h3><p>Description</p><p>Address</p></div>',
replace: true
};
});
myapp.directive("searchResult",function(){
return {
temlate: '<div><h3>name</h3><p>Description</p><p>Address</p></div>'
};
});
myapp.directive("searchResult",function(){
return {
restrict: "AECM",
temlateUrl: "directives/searchResult.tpl.html",
replace: true
};
});
<search-result
student-name={{student.name}}
student-object="student"
student-function="studentFunction(param)">
</search-result>
myapp.directive("searchResult",function(){
return {
restrict: "AECM",
temlateUrl: "directives/searchResult.tpl.html",
replace: true,
scope: {
studentName: "@", // Symbol for creating a text hole
studentObject: "=", // Symbol for object hole
studentFunction: "&", // Symbol for function hole
}
};
});
<search-result
student-name={{student.name}}
student-object="student"
student-function="studentFunction(param)">
<p>*Terms and Conditions Apply.</p>
</search-result>
myapp.directive("searchResult",function(){
return {
restrict: "AECM",
temlateUrl: "directives/searchResult.tpl.html",
replace: true,
scope: {
studentName: "@", // Symbol for creating a text hole
studentObject: "=", // Symbol for object hole
studentFunction: "&", // Symbol for function hole
},
transclude: true
};
});
<!-- This is wrong, it is standard for JS - camel case - but not for html-->
<searchResult someAttribute="#"></searchResult>
<!-- This is correct -->
<search-result some-attribute="#"></search-result>
<h1>Results</h1>
<div>
<h3>name</h3>
<p>Description</p>
<p>Address</p>
</div>
<div>
<h3>name</h3>
<p>Description</p>
<p>Address</p>
</div>
<div>
<h3>name</h3>
<p>Description</p>
<p>Address</p>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment