这是一个显示客户地址的组件(请原谅Jade):
address(style='{{addressStyle}}')
strong {{clinic.businessName}}
br
span {{clinic.address.address1}}
br
span(ng-switch='{{clinic.address.address2 != null}}')
span(ng-switch-when='true')
| {{clinic.address.address2}}
br
span {{clinic.address.suburb}} {{clinic.address.state}} {{clinic.address.postcode}}
br
abbr(title='Phone') P:
| {{functions.formatPhoneNo(clinic.phone)}}
app.directive('clinicAddress', function($interpolate) {
return {
restrict: 'E',
templateUrl: 'directives/clinicAddress',
scope: { clinic: '=', functions: '=' },
link: function(scope, element, attrs) {
scope.addressStyle = attrs.style ? scope.addressStyle = $interpolate(attrs.style)() : '';
}
};
});
另一个基于它,用于显示包含在超链接和地图标记中的客户地址:
a(href='#!/clinic/{{clinic.urlFragment}}')
div(style='width:50px;height:50px;background:url(/img/map/{{index+1}}.png) 190px 30px no-repeat;')
clinic-address(clinic='clinic', functions='functions', style='background:url(/img/map/{{index+1}}.png) 190px 30px no-repeat;')
app.directive('indexedClinicAddress', function() {
return {
restrict: 'E',
scope: { clinic: '=', index: '=', functions: '=' },
templateUrl: 'directives/indexedClinicAddress'
};
});
问题是,当$interpolate
在clinicAddress
' 指令中运行时,index
它是未定义的,因为它在indexedClinicAddress
' 范围内。我怎样才能$interpolate
使用父范围,我假设它是相同的范围indexedClinicAddress
?