我为自举警报创建了自定义指令。我的警报显示正常(硬代码和数据绑定)。根据警报类型,我希望根据返回的范围值(成功,信息,警告,危险)在我的警报消息中显示一个唯一标题。目前,我将该类型传入<h1>
,但我不想要这些值,它们需要定制。AngularJS自定义指令 - 将隔离范围映射到新的子范围
<!-- data binding example -->
<trux-alert ng-repeat="alert in alerts" type="{{alert.type}}" close="closeAlert($index)">{{alert.msg}}</trux-alert>
<!-- hard coded example -->
<trux-alert close="close" type="warning">This is an important warning message!</trux-alert>
在我的指令,范围使用范围隔离:“@”(单程)
.directive('truxAlert', function() {
return {
scope: {
type: '@',
close: '&'
},
replace: true,
transclude: true,
template:
'<div class="alert" '+
'ng-class="[\'alert-\' + (type || \'warning\'), closeable ? \'alert-dismissible\' : null]" '+
'role="alert">'+
'<button ng-show="closeable" '+
'type="button" class="close" '+
'ng-click="close({$event: $event})" '+
'data-dismiss="alert" aria-label="Close">'+
'<span aria-hidden="true">×</span>'+
'<span class="sr-only">Close</span>'+
'</button>'+
'<h1>{{type}}</h1>'+
'<div ng-transclude></div>'+
'</div>',
link: function (scope, element, attrs) {}
}
});
这将是更容易,如果我所有的值是通过数据绑定拉升,但我需要以允许手动硬编码选项。我知道单向隔离范围'@'我不能通过DOM操作来改变这些值。我无法使用'='或'&'进行双向,因为这些值是字符串。
我该如何解决这个问题?
你的意思是说,你想传递硬编码字符串中的'type'值,比如'type =“Something”'? –
是的,类型可以是硬编码类型=“字符串”。但是只有4个公认的价值观。 – CrazyRaider
所以你可以直接读出'attributes'的值,你不需要费心把它传递给隔离的范围。如果它不会动态地生成 –