Answer the question
In order to leave comments, you need to log in
AngularJS: how directives interact - emit, broadcast, controller require?
Good afternoon.
I'm fiddling with AngularJS. I read the documentation, now I'm working on examples from real life in conjunction with Yii
Actually, an example:
There are two directives - a form and a modal window. The form is a wrapper over ActiveForm from Yii.
The form can either exist on its own or be nested in a modal window:
<modal-dialog><br>
<active-form><br>
</active-form><br>
</modal-dialog><br>
angular.module('app').directive 'activeForm', (yiiscripts) -><br>
scope: 'isolated'<br>
replace: false<br>
transclude: false<br>
require: ['activeForm','?^modalDialog']<br>
controller: ($scope, $element) -><br>
onSubmit: ($form, modalDialogController) -><br>
modalDialogController.startLoading() if modalDialogController<br>
$element.submit()<br>
afterValidate: ($form,data,hasErrors,modalDialogController) -><br>
modalDialogController.endLoading() if modalDialogController<br>
modalDialogController.close() if !hasErrors and modalDialogController<br>
!hasErrors<br><br>
link: ($scope, element, attrs, controllers) -><br>
[myController, modalDialogController] = controllers<br>
yiiscripts.activeform element.attr('id'),<br>
afterValidate: ($form, data, hasErrors) -><br>
myController.afterValidate($form,data,hasErrors,modalDialogController)<br><br>
if modalDialogController and modalDialogController.listenSubmit<br>
modalDialogController.listenSubmit -><br>
myController.onSubmit(element, modalDialogController)<br>
angular.module('app').directive 'modalDialog', (templates) -><br>
scope:<br>
title: '@modalTitle'<br>
modalOk: '@modalOk'<br>
modalCancel: '@modalCancel'<br>
replace: true<br>
transclude: true<br>
template: $('#modal_dialog_template').html()<br>
controller: ($scope) -><br>
submitListeners = []<br>
controller =<br>
listenSubmit: (listener) -><br>
submitListeners.push(listener)<br>
onSubmit: (evt) -><br>
evt.preventDefault()<br>
$.each submitListeners, -> this.apply(evt)<br>
startLoading: -><br>
$scope.$broadcast('startLoading')<br>
endLoading: -><br>
$scope.$broadcast('endLoading')<br>
close: -><br>
$scope.$broadcast('close')<br>
controller<br><br>
link: ($scope, element, attrs, controller) -><br><br>
element.css({display:'block'})<br>
.modal(show: false)<br>
$scope.$on 'close', -> element.modal('hide')<br>
$scope.$on 'startLoading', -> element.modal('loading')<br>
$scope.$on 'endLoading', -> element.modal('loading')<br>
$('input[type="submit"]',element).click(controller.onSubmit)<br>
Answer the question
In order to leave comments, you need to log in
It fits perfectly (more: www.egghead.io/video/rzMrBIVuxgM ), only here
template: $('#modal_dialog_template').html()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question