4

I have a custom directive that I would like to inherit the parent scope. I would also like to pass a value via an attribute. It looks like this:

Controller

app.controller('Main', ['$scope', function($scope){
   $scope.cols = { 'col1': true, 'col2':false, 'col3': true};

   $scope.toggleCol = function(colName){
       $scope.cols[colName] = !$scope.cols[colName];
   };
}]);

Directive

wrApp.directive("custTh", function(){
 return {
    restrict: "EA",
    scope: false,
    replace: true,
    template: '<th ng-show="cols[{{ colname }}]" ng-click="toggleCol({{ colname }})">{{ colname }}</th>',

 };
});

HTML

<th cust-th colname="col2"></th>

I just can't seem to get access to the attribute value bc I am inheriting the parent scope. Is it possible to directly access directive attributes from the template?

4 Answers 4

1

You can inherit the scope but also create a child scope by using:

scope: true

Then in order to pass values:

link: function(scope, element, attrs) {
     scope.$watch(attrs.valueFromOutside, function(newValue) {
         scope.valueFromOutside = newValue;
     });
 }

This way in the inner scope of the directive you can have a different value while still having access to the parent scope.

You can see it all in action here: http://jsfiddle.net/HB7LU/15120/

Sign up to request clarification or add additional context in comments.

Comments

0

As long as you don't declare an isolate scope in the directive, you can access the parent scope:

<-- cust-th template can access Main controller's scope -->
<div ng-controller="Main">
  <th cust-th></th>
</div>

There are just a few syntax errors in your code that are preventing you from doing this. In your template, you do not need to interpolate the values being passed into the angular directives (ng-click and ng-show):

<th ng-show="cols[colname]" ng-click="toggleCol(colname)">{{ colname }}</th>   

colname has not been declared as a scope variable in your controller, so when Angular goes to compile your HTML, it will not recognize it. If you would like to continue passing it as an attribute on your HTML element, you'll need to create an angular element in your directive in order to access the value using a link function (See https://docs.angularjs.org/guide/directive). If you want to use interpolation ( {{colname}} ), then you will need to have a variable in your controller like

$scope.colname = 'col1';

1 Comment

I am trying to pass the colname to the directive via an attriubute because that value is isolated to the scope of the directive but I have objects in the parent scope that I need to reference as well.
0

No. You can't access directive attributes while you inherit parent scope. To do this you have to create directive's own scope like below:

app.directive("custTh", function(){
      return {
      restrict: "EA",
      scope: { colname: '@'},   
      template: 'Name : {{ colname }}',
    };
});

and in yout HTML remplate you have to write like this:

<div ng-controller="Main">   
   <cust-th colname="col2" ></cust-th>
</div>

I have also created a fiddle for you help. I you find this useful then please accept this as an answer.

http://jsfiddle.net/HB7LU/10806/

1 Comment

I'm starting to think I'm going to have to rethink my approach but is there a way to reference parent scope using the approach you've described?
0

A template can be a string or a function:

template

HTML markup that may:

Replace the contents of the directive's element (default). Replace the directive's element itself (if replace is true - DEPRECATED). Wrap the contents of the directive's element (if transclude is true). Value may be:

A string. For example <div red-on-hover>{{delete_str}}</div>. A function which takes two arguments tElement and tAttrs (described in the compile function api below) and returns a string value.

See example at use attribute in template

See document at Angular Doc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.