模板
<section id="content" ng-controller="ReservationController as resvnCtrl" >
<form role="form" name="resvnCtrl.form">
<div data-date-picker>
</div>
</form>
</section>
指令
var eateryDirectives=angular.module('eateryDirectives',['eateryControllers']);
eateryDirectives
.directive('datePicker',function(){
var directive={
require:'^resvnCtrl',
link:function(scope,ele,attrs,cntrl){
console.log(cntrl);
}
};
return directive;
});
我让控制器找不到错误。顺便说一句,控制器和diretcive在不同的模块中。那有关系吗?如何从指令访问该控制器?
答案 0 :(得分:2)
如果您想要控制器,那么您应该需要ngController
指令。是的,你可以做到,毕竟这只是正常的指令:
require: '^ngController'
但是,不要这样做,这是完全错误的做法。如果您需要来自此控制器或方法的一些数据,则应使用范围配置并将必要的字段传递到隔离范围:
eateryDirectives.directive('datePicker', function () {
var directive = {
scope: {
data: "=",
onSelect: "&"
},
link: function (scope, ele, attrs, cntrl) {
console.log(cntrl);
}
};
return directive;
});
并在HTML中:
<section id="content" ng-controller="ReservationController as resvnCtrl" >
<form role="form" name="resvnCtrl.form">
<div data-date-picker data="resvnCtrl.dates" on-select="resvnCtrl.update()"></div>
</form>
</section>