Created
May 31, 2015 14:31
-
-
Save alonronin/b157643feac58999a2cc to your computer and use it in GitHub Desktop.
Exposing directive public api to parent controller
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> | |
<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