javascript - How do i can run directive function before controller? -


i need call directive function ( need scope ) before controller.

var app = angular.module('docsrestrictdirective', []);  app.controller('controller', ['$scope',  function($scope ) {   $scope.changederictive(); }]);  app.directive('ngmyder', function() {   return {     restrict: 'a',     compile: function compile(telement, tattrs, transclude) {       return {         pre: function prelink(scope, ielement, iattrs, controller) {           scope.changederictive = function() {              console.log("changed");           };         }       }     }   } }); 

http://plnkr.co/edit/nwb23rscg8zvplugbwh5?p=preview

as requested example ui-router. first define controller base of app.

<body ng-app="myapp" ng-controller="appbasectrl">     <main role="main">         <overlay-spinner></overlay-spinner>         <invite-success></invite-success>         <div ui-view></div>     </main> </body> 

now in ui router define our base route:

.config(function ($stateprovider, $urlrouterprovider) {         $urlrouterprovider.otherwise('/desiredroute');         $stateprovider             .state('desiredroute', {                 url: '/desiredroute',                 templateurl: 'views/pathtotemplate.html',                 controller: 'myviewctrl'             }) }); 

so happen? base controller runs, can initialize desired scope variables, run directives , run our required controller.

so have directive runs before needed controller.

if want cleaner ui-router can define routes this:

in routes config:

  .state('dashboard', {         url: '/dashboard',         templateurl: 'views/templates/dashboard/dashboard-base.html',         controller: 'dashboardbasectrl',         abstract: true     })     .state('dashboard.main', {         url: '/main',         templateurl: 'views/templates/dashboard/dashboard-main.html',         controller: 'dashboardmainctrl'     }) 

then in view dashboard-base:

<div mydirective></div> <div ui-view></div> 

and of course define in base controller ever want , can see... base controller runs directive our desired controller directive runs before controller...

edit

i have created small plunker asked... see here no timeout directive called before our main controller using base controller example first example in answer

plunker answer


Comments