To avoid using $timeout , the directive can notify the controller when everything is ready. Take a look:
.directive('modal', function ($uibModal) { var directive = {}; directive.restrict = 'EA'; directive.scope = { control: '=', onReady: '&' // <-- bind `onReady` with `onModalReady` }; directive.link = function (scope, element, attrs) { scope.control = scope.control || {}; scope.control.openModal = function() { scope.modalInstance = $uibModal.open({ template: '<button ng-click="close()">Close</button>', scope: scope }) }; scope.close = function () { scope.modalInstance.close(); }; scope.onReady(); // <-- notify controller }; return directive; });
HTML output:
<div modal on-ready="onModalReady()" control="modalCtl"></div>
Our controller:
$scope.onModalReady = function(){ $scope.modalCtl.openModal(); }
Modified Plunger
About comments @Eduardo La Hoz Miranda
You must be ok with a timeout. I would reduce the time to 0, tho, since timeout will send your call to the end of the event loop.
Usually, when we initialize $timeout with 0 milliseconds or without an argument:
$timeout(function(){ $scope.modalCtl.openModal(); });
We hold $scope.modalCtl.openModal() to run until the next digest ae last in the queue loop. Therefore, in this case, the directory link will work from start to finish and only after you enter $timeout .
The $ timeout may work in the development environment, but may not work.
In the Production section, you have the same code. It should work. I believe the problem is something else. If you are not sure about $timeout , use the above method that I posted.
Recorded plunger