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?
I think you have to use the
transclude
option
. In fact, asAngularJS
docs says :Because of the
Directives
isolatedscope
that you createdMore docs at:
http://docs.angularjs.org/guide/directive
When angular compiles an element with isolated scope it has some rules:
If you want to alter this behavior you can pass the isolated scope to the tranclusion function like so:
I highly recommend you to see these tutorials:
And to read more:
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?