angularjs: using a directive inside the ui-bootstr

2019-07-21 21:11发布

问题:

I can't figure out how to call a directive from within a modal created with the $dialog service. That directive should also be able to see the buttons on the modal and override their ng-click action.

Here's my modal template:

<div class="modal-header">
<h1>Rechercher</h1>
</div>
<div class="modal-body">

  <search-person></search-person>

</div>
<div class="modal-footer">
  <button ng-click="close(result)" class="btn btn-primary">Close</button>
</div>

the searchPerson directive template:

<span>{{test}}</span>

the searchPerson directive itself:

angular.module('person.directives').directive("searchPerson", ['PersonService',    function (PersonService) {
return {
    restrict: "E",
    templateUrl: "person/views/searchPerson.html",
    scope: {},
    controller: 'searchPersonCtrl'
}
}]);

the searchPerson controller:

angular.module('person.controllers').controller('searchPersonCtrl', ['$scope', function ($scope) {
   $scope.test = 2;    
}]);

and finally the modal controller:

 angular.module('person.controllers').controller('DialogController', ['$scope', 'dialog', function($scope, dialog) {
    $scope.test = 2;
    $scope.close = function (result) {
       alert('modal scope');
       dialog.close($scope.test);
    };
 }]);

So how do I make the searchPerson controller and the modal controller communicate with each other ?

回答1:

I think I went a step too far. Instead of having a template and a controller for Modal, and a directive inside, the modal is now the template for the directive. Here's the code:

<div class="modal-header">
<h1>Rechercher</h1>
</div>
<div class="modal-body">

<!-- this used to be the searchPerson directive but now the Modal and the directive are just the same directive -->
<span>{{test}}</span>


</div>
<div class="modal-footer">
   <button ng-click="close(result)" class="btn btn-primary">Close</button>
</div>