下面是显示客户的地址组件(请原谅Jade):AngularJS嵌套组件和范围
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
的范围相同?
谢谢!它的工作,这很简单,我学到了一些东西。 –
有趣的是,当浏览器尝试获取样式中未插入的资源时,我仍然收到404。我认为这一直在抛我。现在我仍然看到它,我认为这只是浏览器做的事情,因为这个属性被称为'style'。 –
理论证实。现在使用'elementStyle'而不是'style'来防止向服务器发送不必要的请求,否则将以'404'响应。 –