icode icode - 1 year ago 127
AngularJS Question

angularjs controller access directive scope value

I have this controller with directive inside my html code. The directive have scope value I would like access from the parent controller and show in the html. Hope my example code give you a simple overview of what I'm trying to achieve.

I know this work using $rootScope.showvalue = 'pass text'; in the directive works but don't know if this is the best solution.

app.directive('customdir', [function() {
return {
restrict: "E",
scope: {
showvalue: "="
link: function(scope, el, attrs) {
scope.showvalue = 'pass text';


app.controller('mycontroller', function($scope) {

<div ng-controller="mycontroller">

/* Show scope value from custom dir */


Answer Source

Since you're binding the directive's isolate scope showvalue property via bidirectional binding, you can simple pass the parent scope property in as an attribute

<customdir showvalue="showvalue"></customdir>


Demo ~ http://plnkr.co/edit/W5k0hgDElklseOvjwRsS?p=preview

If you want the HTML to be <custom-dir>, you'll need to rename your directive to 'customDir'.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download