Reputation: 125
html:
<!doctype html>
<html>
<head>
</head>
<body>
<div ng-app="project">
<div ng-controller="mainCtrl">
{{ property1 }}
<br />
{{ property2 }}
<div class="ts" d-child property1="{{ property1 }}cloud" property2="property2">
property1: {{ property1 }}
<br />
property2: {{ property2 }}
</div>
</div>
</div>
</body>
</html>
js:
angular.module('project', [])
.controller('mainCtrl', function($scope) {
$scope.property1 = 'ss';
$scope.property2 = 'dd';
});
angular.module('project')
.directive('dChild', function() {
return {
restrict: 'A',
scope: {
property1: '@',
property2: '='
},
link: function(scope, element, attrs) {
}
// template: '<input type="text" ng-model="property1" />'
}
})
I thought the property1: {{ property1 }}
would be "property1: sscloud",but it turns out to be "ss",as if it still refers to the scope of the mainCtrl
controller, shouldn't it be refer the scope of the d-child
directive?
if I use template in the directive,it does refer to the right scope and shows 'sscloud',anyone can tell me why?
Upvotes: 4
Views: 609
Reputation: 32397
angular.module('project')
.directive('dChild', function() {
return {
restrict: 'A',
transclude: true,
scope: {
property1: '@',
property2: '='
},
link: function(scope, element, attrs, ctrl, linker) {
linker(scope, function(clone, scope){
element.append(clone)
})
}
}
})
Upvotes: 5
Reputation: 4656
I think you have to use the transclude
option
.
In fact, as AngularJS
docs says :
What does this transclude option do, exactly? transclude makes the contents of
a directive with this option have access to the scope outside of the directive
rather than inside.
Because of the Directives
isolated scope
that you created
More docs at:
http://docs.angularjs.org/guide/directive
Upvotes: 0
Reputation: 699
I'm not quite sure about this, I'm pretty sure it all has to do with when each {{}} is evaluated, and when the scope of the directive becomes isolated. My suggestion is to place the content in a template, as it seems to be working when doing so.
If you want to read more about the difference of of "@" and "=" in directive scopes, here's the best text I've found about it.
What is the difference between '@' and '=' in directive scope in AngularJS?
Upvotes: 0