2

Unknown provider: $confirmModalProvider <- $confirmModal <- confirmModalCtrl注入錯誤,使用ui-bootstrap

爲什麼我得到這個錯誤?我正在嘗試使用AngularJS UI Bootstrap來打開模式並獲得結果。當我觸發$scope.deleteQuestion()時,出現此錯誤。任何想法我在這裏做錯了嗎?

var crtPromoCtrl = angular.module('crtPromoCtrl', ['crtPromoSrv']); 

crtPromoCtrl.controller('surveyCtrl', ['$scope', '$modal', 'surveySrv', function($scope, $modal, surveySrv) 
{ 
     $scope.questions = surveySrv.getQuestions(); 

    $scope.editQuestion = function(index) 
    { 
     surveySrv.setEditQuestion(index); 
    }; 

    $scope.deleteQuestion = function(index) 
    { 
     var confirmModal = $modal.open({ 
      templateUrl: 'confirm-delete.html', 
      controller: 'confirmModalCtrl', 
      size: 'sm' 
     }); 

     confirmModal.result.then(function(msg) 
     { 
      console.log(msg); 
     }); 

     return false; 
    }; 
}]); 

crtPromoCtrl.controller('confirmModalCtrl', ['$scope', '$confirmModal', function($scope, $confirmModal) 
{ 
    $scope.yes = function() 
    { 
     $confirmModal.close('yes'); 
    }; 

    $scope.no = function() 
    { 
     $confirmModal.dismiss('no'); 
    }; 
}]); 

編輯:https://angular-ui.github.io/bootstrap/#/modal

+0

是什麼'$ confirmModal'的控制器?你想訪問'crtPromoCtrl'的confirmModal' –

+0

請參閱腳本:https://angular-ui.github.io/bootstrap/#/modal。這就是我想要做的。 – jstudios

+0

它是'$ confirmModal'還是'confirmModal'? –

回答

2

你第二控制器應使用$modalInstance代替$confirmModal

請注意,$ modalInstance代表一個模式窗口(實例) 依賴。

代碼

crtPromoCtrl.controller('confirmModalCtrl', ['$scope', '$modalInstance', function($scope, $modalInstance) { 
    $scope.yes = function() { 
     $modalInstance.close('yes'); 
    }; 

    $scope.no = function() { 
     $modalInstance.dismiss('no'); 
    }; 
}]); 
+0

這沒有竅門!我認爲'modalInstance'這個名字是可以互換的,只要它在第一個控制器中加入var名稱即可。謝謝! – jstudios

+0

@jstudios很高興在這裏它幫助你,謝謝:-) –