0

This is my directive. It works, But when I uncomment scope{} - DOM elements do not see scope.greeting.

app.directive('myDir', function () {
            return {
                restrict: 'E',
                //scope:{},
                compile: function(tHtml,tAttr){
                    return {
                        post: function(scope,ele,attr){
                            scope.greeting = 'Hello!';
                        }
                    }
                }
            };

If to use the common way witk link function then all works. At the same time I cannot use Compile and Link at the same time What is wrong?

Maher
  • 2,517
  • 1
  • 19
  • 32
Lagoda Denis
  • 235
  • 5
  • 13

1 Answers1

0

When using scope property you are saying that you want to use an isolated scope.

Because myDir does not have greeting property defined, when you uncomment scope you get undefined forn greeting. With //scope: {} you are using parent scope (which, I guess, has got greeting property).

See this What is the difference between '@' and '=' in directive scope in AngularJS?.

Chris Hermut
  • 1,708
  • 3
  • 17
  • 32
  • but separate Link function works. By idea Compile.Post should return the same Link function. Ok - how to using isolated scope to make DOM call function in post function? – Lagoda Denis Jan 25 '18 at 08:52
  • Read about DDOs https://docs.angularjs.org/api/ng/service/$compile#directive-definition-object (scope section). Also you rcompile function arguments are incorrect. – Chris Hermut Jan 25 '18 at 09:00
  • Ok - will ask another way : Compile.Post and just Link are the same? b/c I see they works differently. – Lagoda Denis Jan 25 '18 at 09:04