Angular 的新手,只是想与 Zurb Foundation 4 和谐相处。一个恰当的例子;我正在尝试使用http://foundation.zurb.com/docs/components/reveal.html组件。
直截了当的方法似乎是包装为指令:
directive('modal', function() {
return {
template: '<div ng-transclude id="notice" class="reveal-modal">' +
'<a close-modal></a>' +
'</div>',
restrict: 'E',
transclude: true,
replace: true,
scope: {
'done': '@',
},
transclude: true,
link: function(SCOPE, element, attrs, ctrl) {
SCOPE.$watch('done', function (a) {
// close-modal
});
}
}
}).
directive('closeModal', function() {
return {
template: '<a ng-transclude href="#" class="close-reveal-modal">x</a>',
restrict: 'A',
transclude: true,
replace: true
}
}).
directive('showModal', function() {
return {
template: '<a ng-transclude class="reveal-link" data-reveal-id="notice" href="#"></a>',
restrict: 'A',
transclude: true,
replace: true,
}
});
这在某种程度上可以正常工作,例如,我可以使用模态显示来自模板的不同通知:
<modal done="">
<div ng-include src="'partials/notices/' + notice + '.html'"></div>
</modal>
<select ng-model="notice" ng-options="n for n in ['notice-1', 'notice-2']">
<option value="">(blank)</option>
</select>
<a show-modal>show modal</a>
但是,如果我想在某个事件(例如,在某个事件中)从控制器触发关闭模式/显示模式,它就会变得棘手$watch
。我假设我的指令需要一个控制器来触发点击,但是 Angular 的做法会很好吗?