1.2.0: Issue with isolate scope change
I have an issue with this latest change:
due to d0efd5ee, Child elements that are defined either in the application template or in some other directives template do not get the isolate scope. In theory, nobody should rely on this behavior, as it is very rare - in most cases the isolate directive has a template.
The issue I have is that is that sometimes a directives template should be inline because it will vary from instance to instance. For example I have a tabs directives that can be completely different from instance to instance but it has common functionality that needs an isolate scope.
Here is how the directive is written today:
http://plnkr.co/edit/1FTnEhnXIDdpXC9om1ds?p=preview
The isolate scope is created (the alert shows an $id of 004) but not assigned to the element since the directive displays 003, the controllers scope (it seems weird that an isolate scope would be created if it is not going to be used).
Now the only other way I though this would work is by using transclusion so I tried this:
http://plnkr.co/edit/8wFayf1DwhIx0EtXG8b0?p=preview
In this case the alert still displays 004 but the directive displays 005 which still causes the tabs to not work properly.
Is there another way I missing to convert this directive to work for 1.2.0?
Check Breaking Changes in the chang log: https://github.com/angular/angular.js/blob/master/CHANGELOG.md
At the top of the issue, I pasted the change from the changelog that I think is causing this but the change log offer no alternative other than "In theory, nobody should rely on this behavior, as it is very rare" but i guess I am the rare case.
I am just looking for how I can use this directive with 1.2.0
@ryanzec Just don't use transclusion there , and put the transcluded dom in to template and your directive will work.
http://plnkr.co/edit/RYlNZ43HXMYLHnghMIEp?p=preview
@igorzg Thanks for pointing me in the right direction. I experimented with the templateUrl function and found out that I have access to the attributes in it so I found a solution I am happy with (one that allows me to be able to have 1 directive for tabs but use multiple templates like the old code does). My ending solution is this:
http://plnkr.co/edit/HWe8vwcmUNiPMNH9H9C3?p=preview
I also have a directive which receives various templates. It's actually a datasource directive that had either a dropdown, a checkboxlist or a radio button group. They all had in common the items collection provided by the datasource directive. Now it's broken. I must be the other rare case :)
This change caused us some trouble in our dev environment too. We have a similar case (to the tabs example) where our design team can design mix-and-match "search tools" on the fly simply by defining an initial wrapping directive which the other dropdowns, selectors and live user feedback get output within. But since our sites run on a central platform where adding one-off HTML files would be a pain to manage, we'll have to rethink this structure a bit.
I may have found the solution. @ryanzec gave me the idea when I saw how he used a function for templateUrl. Same it's possible for template. So I've ended up with:
template: function (tElement, tAttrs) {
return tElement.html();
}
Note that this will not work if the isolated scope is used in the same element as the directive.
Thanks @bogdan-litescu , your snippet fixed my problem.
My html is like this:
<tr directive-with-isolated-scope>
<td ng-repeat="k in abc">{{k}}</td>
</tr>
where abc is defined in directive-with-isolated-scope.