问题
Wondering if someone can explain the current behavior.
I have one directive with isolate scope, call it dirA. I then put another directive on it, dirB. Expecting the second directive to get each scope that the isolate directive creates.
dirB gets the controller scope. Why would it not get the same isolate scope that dirA is creating for that element?
Thanks for any help. I don't really have any code for this, just curious. If anyone needs two little directives and some html to help visualize i will of course. thank you.
回答1:
What you observe is correct and this behavior is by design.
When a directive requests an isolated scope, AngularJS creates a scope for that directive specifically, not for the element where the directive is declared on. In other words, the isolation is per-directive, not per-element. This makes coding reusable and standalone directives much easier than if one has to guard against improper usage of the isolated scope from other directives.
All other directives declared on the same element share the outer scope. (It's also worth noting that these directives can't even request their own scope, isolated or not. While this behavior is not guaranteed prior to 1.3 due to a bug, it is already fixed in 1.3.)
来源:https://stackoverflow.com/questions/26692263/angularjs-multiple-directives-on-element-with-one-being-isolate-scope