获取方向childs标签AngularJS
我做了这个指令,我在html中调用它,就像获取方向childs标签AngularJS,angularjs,angularjs-directive,angularjs-scope,Angularjs,Angularjs Directive,Angularjs Scope,我做了这个指令,我在html中调用它,就像 <my-datepicker /> 我现在想做的是把它花掉,这样我就能做这样的事情了 <my-datepicker > <my-day>{{date}}</my-day> <my-month>{{moth}}</my-month> <my-year>{{year}}</my-year> </my-datepicker>
<my-datepicker />
我现在想做的是把它花掉,这样我就能做这样的事情了
<my-datepicker >
<my-day>{{date}}</my-day>
<my-month>{{moth}}</my-month>
<my-year>{{year}}</my-year>
</my-datepicker>
{{date}}
{{moth}}
{{year}
导致我的datepicker指令可以获取子元素
你怎么能做到这一点?您是否需要创建类似于childs的standanlone指令,一个如何在parent(my datepicker)指令中从它们获取值的指令
app.directive('myDay', function () {
return {
restrict: 'E',
require: '^myDatepicker',
link: function (scope, element, attrs, parentCtrl) {
parentCtrl.doSomething();
}
};
});
在父项中:
app.directive('myDatepicker', function () {
return {
restrict: 'E',
controller: function () {
this.doSomething = function () {
alert('Do something in parent');
};
}
};
});
我认为扩展指令的最好方法是将参数(日期-月份ec…)作为属性传递 你的html应该是这样的
<my-datepicker day="day" month="month" year="year"/>
}) 谢谢你的回答。我理解你为什么这么想。但是我想用另一种方式来做的原因是因为我要对那些标记进行一些html呈现和样式设计,我认为这就是我想要的。试一试后我会汇报的。谢谢
function myDatepicker() {
var directive = {
templateUrl: 'yourtemplate.html',
restrict: 'E',
link: link,
scope: {
date: '=',
month: '=',
year: '='
}
}
return directive;
function link(scope, element, attrs) {
// use here scope.date scope.month and scope.year
}