Skip to content

Instantly share code, notes, and snippets.

@alonronin
Created May 31, 2015 14:31
Show Gist options
  • Save alonronin/b157643feac58999a2cc to your computer and use it in GitHub Desktop.
Save alonronin/b157643feac58999a2cc to your computer and use it in GitHub Desktop.
Exposing directive public api to parent controller
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
<meta charset="utf-8">
<title>My App</title>
</head>
<body ng-app="MyApp">
<div ng-controller="AppCtrl as app">
<h2>{{app.title}}</h2>
<button ng-click="app.editor.open()">Open</button>
<editor public-api="app.editor" />
</div>
<script id="jsbin-javascript">
angular.module('MyApp', [])
.controller('AppCtrl', function($scope){
this.title = "My App";
})
.directive('editor', function(){
return {
restrict: 'E',
scope: {
publicApi: '='
},
controller: function(){
this.open = function(){
alert('Opened');
}
},
controllerAs: 'editor',
bindToController: true,
link: function(scope, el, attrs, ctrl){
scope.editor.publicApi = scope.editor
}
}
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment