2016-05-29 91 views
3

我嘗試定義控制器的對話,如角材料的例子(https://plnkr.co/edit/DlhNXU問題注入控制器在角JS

var TestApp = angular.module('TestApp', ['ngMaterial']); 

TestApp.controller('MainController', function MainController($scope, $mdDialog, $mdMedia, DialogController) { 
    ... 
    $mdDialog.show({ 
     controller: DialogController, 
     ... 
    }); 
    ... 
} 

但我總是得到這樣看出:

Error: [$injector:unpr] Unknown provider: DialogControllerProvider <- DialogController <- MainController

回答

0

您無法注入控制器。因此,從依賴注入參數MainController刪除DialogController和使用controller: 'DialogController'的對話框:

TestApp.controller('MainController', function MainController($scope, $mdDialog, $mdMedia) { 

    // ... 

    $scope.showImprintDialog = function(ev) { 
    var useFullScreen = ($mdMedia('sm') || $mdMedia('xs')) && $scope.customFullscreen; 
    $mdDialog.show({ 
     controller: 'DialogController', 
     templateUrl: 'impressum.html', 
     parent: angular.element(document.body), 
     targetEvent: ev, 
     clickOutsideToClose: true, 
     fullscreen: useFullScreen 
     }) 
     .then(function(answer) { 
     $scope.status = 'You said the information was "' + answer + '".'; 
     }, function() { 
     $scope.status = 'You cancelled the dialog.'; 
     }); 
    $scope.$watch(function() { 
     return $mdMedia('xs') || $mdMedia('sm'); 
    }, function(wantsFullScreen) { 
     $scope.customFullscreen = (wantsFullScreen === true); 
    }); 
    }; 

    // ... 

}); 

演示:https://plnkr.co/edit/46YtlQbndPQ4n27uGFGQ?p=preview

+0

但是有一個問題,想放控制器在一個外部文件。 – n00n

+0

但我有另外一個問題,想外包控制器...但它無法找到他們了...... https://plnkr.co/edit/wPIsxH – n00n

+0

對不起,蝙蝠的說明「但它不能再找到他們。」我得到了一個錯誤消息「angular.js:13424錯誤:[ng:areq]參數'DialogController'不是一個函數,沒有定義」 – n00n