我试图想出一个可重用指令库。我试图实现的前两个指令是DatePicker和DateRangePicker。 DateRangePicker必须包含两个DatePickers。创建包含其他指令的Angular.js指令
我想要的DatePicker有类似签名:
<div cx-date-picker="" cx-label="myLabel"
cx-id="myDate" cx-source="myDateVarInScope"></div>
,我想DateRangePicker看起来像这样:
<div cx-date-range-picker cx-id="searchRangePicker"
cx-source="myDateRangeInScope"></div>
其中myDateRangeInScope包含的成员:的startDate和结束日期
我看了几个如何创建指令的例子,但我不知道如何将参数传递到基本指令。这里是显示适当的值theId和theLabel但不显示正确的日期的DatePicker
angular.module('ng').directive('cxDatePicker', function() {
return {
restrict: 'A',
scope: 'isolate',
template: '<div class="control-group input-append">' +
'<label for="{{theId}}" class="label">{{theLabel}}</label>' +
'<input id="{{theId}}" class="input-small" type="text" ' +
'ng-model="theSource" data-date-format="dd/mm/yyyy" bs-datepicker>' +
'<button type="button" class="btn" data-toggle="datepicker">' +
'<i class="icon-calendar"></i></button>' +
'</div>',
link: function (scope, iterStartElement, attr) {
var theId = attr['cxId'];
scope.theLabel = attr['cxLabel']
scope.theId = attr['cxId'];
scope.theSource = attr['cxSource'];
}
};
});
的代码。
这是DateRangePicker的代码,它无法为基础DatePickers设置属性。
angular.module('ng').directive('cxDateRangePicker', function() {
return {
restrict: 'A',
scope: 'isolate',
template: '<div cx-date-picker="" cx-source="{{startSource}}" ' +
'cx-label="{{fromLabel}}" cx-id="{{startId}}"></div>' +
'<div cx-date-picker="" cx-source="{{endSource}}" cx-label="{{toLabel}}" ' +
' cx-id="{{endId}}"></div>',
link: function (scope, iterStartElement, attr) {
var theId = attr['cxId'];
scope.startId = theId + "From";
scope.endId = theId + "To";
scope.fromLabel = "From";
scope.toLabel = "To";
scope.startSource = attr['cxSource'] + ".startDate";
scope.endSource = attr['cxSource'] + ".endDate";
}
};
});
任何人都可以指出我的解决方案吗?我在DateRangePicker的link()方法之前调用了基础DatePickers的link()方法。因此难怪价值观没有通过。但我缺乏整体的概念理解来解决问题。官方文件没有多大帮助。
一般来说,有没有人试图达到类似的目标 - 在其他指令之上构建指令,并通过这样做,构建一个业务领域特定的组件库?
http://www.youtube.com/watch?feature=player_embedded&v=WqmeI5fZcho#! 这是一个不错的视频,它很好的阐明了 – mkvakin
为什么不使用Element指令而不是Attribute? –