Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Isolate scope not working without templateUrl/template #13556

Closed
llafuente opened this issue Dec 16, 2015 · 5 comments
Closed

Isolate scope not working without templateUrl/template #13556

llafuente opened this issue Dec 16, 2015 · 5 comments

Comments

@llafuente
Copy link

Affect all versions, I can't find any issue related.

Test: http://plnkr.co/edit/x51ohRcoM8uCuaHpIloB?p=preview
To toogle the error go to script, un/comment line 34

The following directive, do not create a isolated scope

.directive('isolateEmpty', function() {
  return {
    restrict: 'EA',
    scope: {},
    link: function($scope, element, attrs) {}
  };
})

But this one will create the scope as expected.

.directive('isolateEmpty', function() {
  return {
    restrict: 'EA',
    scope: {},
    templateUrl: "xxx.html",
    link: function($scope, element, attrs) {}
  };
})

I try to test the other scope combo (true/false) but i'm not sure if it's working... Can't find a proper test...

@gkalpak
Copy link
Member

gkalpak commented Dec 16, 2015

I am not sure why this happens, but it seems to be by design. It was a breaking change back in v1.2.0 (it is mentioned in the changelog).

As per this comment:

We only pass the isolate scope, if the isolate directive has a template, otherwise the child elements do not belong to the isolate directive.

Like I said, I don't know why this is so (it seems counter-intuitive), but it is like that since (almost) forever. Anyone knows (or can find out) what was the reasoning behind this change ?

@gkalpak
Copy link
Member

gkalpak commented Dec 16, 2015

BTW, you can achieve what you want (but in a more verbose way) by using transclusion and "manually" managing the transclusion scope:

.directive('isolateEmpty', function isolateEmptyDirective() {
  return {
    scope: {},
    template: '',
    transclude: true,
    link: function isolateEmptyPostLink(scope, elem, attrs, _, transclude) {
      ...
      transclude(scope, function isolateEmptyCloneAttachFn(clone) {
        elem.append(clone);
      });
    }
  };
}

Demo plnkr

@Narretz
Copy link
Contributor

Narretz commented Dec 16, 2015

Afaik, isolated scopes were intended for "component" directives, i.e. those that bring their own template. Although I've sometimes wanted to use the element children as an ad-hoc template so to say. It works with transclude, so maybe that's the way to go.

@gkalpak
Copy link
Member

gkalpak commented Dec 16, 2015

Indeed it doesn't sound like a good practice to rely on a component-directive's internals from the HTML (i.e. have child DOM elements that assume certain things about a directive on their parent) - if the contents are specific to a component, they belond in it's template.

In that respect, it does sounds reasonable.

@Narretz
Copy link
Contributor

Narretz commented Dec 16, 2015

Let's clarify that in the docs sometime ..

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants