Skip to content

Instantly share code, notes, and snippets.

@nopium
Last active October 16, 2016 08:01
Show Gist options
  • Save nopium/0af54d1dd2ea6096ad1e1352ecbb0b96 to your computer and use it in GitHub Desktop.
Save nopium/0af54d1dd2ea6096ad1e1352ecbb0b96 to your computer and use it in GitHub Desktop.
Angular Tips
Directives
==========
Directive calls controller
-------------------------
app.controller("AppCtrl", function($scope) {
$scope.func = function() {
alert("Controller's function published on scope");
}
}
app.directive(“enter”, function(scope, el, attr) {
element.bind(“mouseenter”, function() {
scope.$apply(attr.enter);
}
}
<div ng-controller="AppCtrl">
<div enter="func()">Put your mouse here to call controller's func from directive</div>
</div>
Directive calls directive
-------------------------
app.directive("parentDirective", function(){
return {
scope: {}, //! isolates scope
controller: function($scope) {
$this.func1 = function(){}...
},
link: function(scope, element) {
element.addClass(...)
element.bind("event", function(){...})
}
}
});
app.directive("childdirective", function() {
return {
require:"parentdirective",
link: function(scope, element, attrs, parentCtrl){
parentCtrl.func1(...)
}
}
}
ht
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment