0

Please refer to code below.

I'd like to avoid using ng-trasclude, as it's extra wrapping elements, + ng-transclude make own scope. So my goal is to render <div foo title="FOO!">FOO!</div> in the end.

$compile(el.html())(scope) breaks, since again, it needs a wrapping element.

template: "<div ng-transclude></div>" will fail to acces scope.title.

Thanks

EDIT Added plunker link: https://plnkr.co/edit/R1CAc5pksOVMJoFLhsTu?p=preview And snippet

angular.module('app', []).directive('foo', function() {
    return {
        restrict: 'A',
        scope: {
            title: '@'
        }
    }
});
<!DOCTYPE html>
<html ng-app="app">
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.min.js"></script>
  </head>
  <body>
    <div foo title="FOO">{{title}}</div>
    <span>expecting "FOO!" above this line, but, sigh...</span>
  </body>
</html>

EDIT 2

I'd like to keep isolate scope, so that attributes (<div foo title="FOO!">{{title}}</div> are then applied via scope: {title:'@'}.

EDIT 3

Updated the snippet.

3 Answers 3

1

You assigned isolated scope for the directive. if you want access it make it as local by given scope : true.

angular.module('app', []).directive('foo', function() {
  
    return {
        restrict: 'A',
         scope: {
            title : '='
        },
        template : '{{title}}'
    }
});
<!DOCTYPE html>
<html ng-app="app">
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.2/angular.min.js"></script>
  </head>
  <body>
    <div foo title="'Foo!'"></div>
    <span>expecting "FOO!" above this line, but, sigh...</span>
  </body>
</html>

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

3 Comments

Thanks, yeah that way I would need to manually assign attributes to the scope , i.g: $scope.title = $attrs.title;, while instead I could use <div foo title="FOO!">{{title}}</div> along with scope: {title: '@'}.
Updated question.
@AleksandrMakov - I updated the answer you just check
0

you need to have a template to render scope.foo so in your directive you need to specify a template. also if you aren't going to have any values coming from the parent you don't have to put scope: {} as a scope will be create for you already

angular.module('app', []).directive('foo', function() {
    return {
        restrict: 'A',
        template: '<span ng-bind="foo"></span>', // <-- add this line
        link: function(scope, el) {
            scope.foo = 'FOO!';
        }
    }
});

Comments

0

try with it

 link: function(scope, el,attr) {
            scope.$parent.foo = "foo"
            scope.foo = 'FOO!';
          }

1 Comment

You must provide some explanation in your answer.

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.