2016-03-26 77 views
0

我現在有點困惑。我寫了一個指令,它基本上根據一些「狀態」打開不同的模態。 出於某種原因,「鏈接」功能不再執行,它恢復了一段時間。我不知道什麼時候停止工作。也許一些解決不好的合併衝突,也許一些角度較小的更新? 希望任何人都能幫助奧德向正確的方向提示我。當控制器存在時,不會執行angularjs鏈接功能

鏈接函數被調用,其中沒有控制器存在,否則它不會被執行。註釋掉控制器的邏輯也不會導致鏈接的執行。

<hcd-modal-messages status="status" redirect="app.dashboard.index"></hcd-modal-messages> 

指令:

angular 
    .module('hcdProtocol') 
    .directive('hcdModalMessages', function() { 
     return { 
      restrict: 'E', 
      templateUrl: 'app/protocols/components/modalMessages/modalMessages.html', 
      scope: { 
       labelSuccess: '@labelSuccess', 
       labelProcess: '@labelProcess', 
       labelFailed: '@labelFailed', 
       labelCached: '@labelCached', 
       status: '=status', //values: HIDDEN, PROCESS, SUCCESS, FAIL 
       redirect: '=redirect', 
       delay: '@redirectDelay' //delay in milliseconds 
      }, 
      controller: ['$scope', '$timeout', '$state', function($scope, $timeout, $state) { 
      $scope.processModal = new Foundation.Reveal(
       angular.element(angular.element(".modal-process")[0]), 
       { 
        closeOnClick: false 
       } 
      ); 
      $scope.successModal = new Foundation.Reveal(
       angular.element(angular.element(".modal-success")[0]), 
       { 
        closeOnClick: false 
       } 
      ); 
      $scope.failModal = new Foundation.Reveal(
       angular.element(angular.element(".modal-fail")[0]), 
       { 
        closeOnClick: true 
       } 
      ); 
      $scope.offlineModal = new Foundation.Reveal(
       angular.element(angular.element(".modal-cached")[0]), 
       { 
        closeOnClick: false 
       } 
      ); 

      $scope.$watch('status', function(status) { 
       //close all modals 
       $scope.processModal.close(); 
       $scope.successModal.close(); 
       $scope.failModal.close(); 
       //open modal corresponding to current status 
       switch(status) { 
        case 'PROCESS': 
         $scope.processModal.open(); 
         break; 
        case 'SUCCESS': 
         $scope.successModal.open(); 

         if(typeof $scope.redirect !== 'undefined') { 
          //delay redirection 
          $timeout(function() { 
           $state.go($scope.redirect); 
          }, $scope.redirectDelay); 
         } 
         break; 
        case 'OFFLINE_CACHED': 
         $scope.offlineModal.open(); 

         if(typeof $scope.redirect !== 'undefined') { 
          //delay redirection 
          $timeout(function() { 
           $state.go($scope.redirect); 
          }, $scope.redirectDelay); 
         } 
         break; 
        case 'FAIL': 
         $scope.failModal.open(); 
         break; 
       } 
      }); 

      }], 
      link: function(scope, element, attrs, controller, transcludeFn) { 
       //set default label 
       if(typeof attrs.labelProcess == 'undefined') scope.labelProcess = "Creating new protocol"; 
       if(typeof attrs.labelSuccess == 'undefined') scope.labelSuccess = "successfully created"; 
       if(typeof attrs.labelFailed == 'undefined') scope.labelFailed = "creation failed"; 
       if(typeof attrs.labelCached == 'undefined') scope.labelCached = "Created in offline cache"; 

       scope.redirectDelay = (typeof attrs['redirect-delay'] === 'undefined') ? 3000 : attrs['redirect-delay']; 
       scope.redirect = (typeof attrs.redirect === 'undefined') ? false : attrs.redirect; 

       //close modals when directive ist destroy to avoid grey (ui blocking) overlay 
       scope.$on('$destroy', function() { 
        scope.processModal.close(); 
        scope.failModal.close(); 
        scope.successModal.close(); 
        scope.offlineModal.close(); 
       }); 
      } 
     } 
    }); 

我目前使用AngularJS V1.5.3

我快速修復:我最終把控制器邏輯到我的鏈接功能。其中大部分屬於首先。所以現在工作了,因爲我沒有控制器,但我仍然沒有理解爲什麼控制器和鏈接沒有在我的情況下執行。

回答

0

使用此格式

angular 
    .module('module') 
    .controller('MyCtrl',/*your controller*/) 
    .directive('myDirective', function() { 
     return { 
      require: '^^MyCtrl', 
      link: function (scope, el, attrs, MyCtrl) /*....*/ 
     } 
    }) 
+0

這導致 「控制器 'hcdModalMessagesController',由指令 'hcdModalMessages' 要求,無法找到!」 – noisy

相關問題