Both scope: true
and scope:{}
will create a child scope for the directive. But,
scope:true
will prototypically inherit the properties from the parent(say the controller where the directive comes under) where as scope:{}
will not inherit the properties from the parent and hence called isolated
For instance lets say we have a controller c1 and two directives d1 and d2,
app.controller('c1', function($scope){
$scope.prop = "some value";
});
.directive('d1', function() {
return {
scope: true
};
});
.directive('d2', function() {
return {
scope: {}
};
});
<div ng-controller="c1">
<d1><d1>
<d2><d2>
</div>
d1(scope:true) will have access to c1 scope -> prop where as d2 is isolated from the c1 scope.
Note 1: Both d1 and d2 will create a new scope for each directive defined.
Note 2: Apart from the difference between the two, for scope:true
- Any changes made to the new child scope will not reflect back to the parent scope. However, since the new scope is inherited from the parent scope, any changes made in the c1 scope(the parent scope) will be reflected in the directive scope.
Tip: Use scope:{}
or isolated scope
for reusable angular directives. So that you won't end up messing with the parent scope properties
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…