2016-01-13 102 views
6

我想將某些數據傳遞給$ mdDialog。事實上,我有兩個控制器分開的文件。這裏是我的控制器代碼

function openDialog(id) { 
     $mdDialog.show({ 
      locals:{ 
       profileId: id 
      }, 
      controller: ['$scope', 'profileId', function($scope, profileId) { 
       var self = this; 
       self.profileId= profileId; 
      }], 
      controllerAs: 'profileCtrl', 
      templateUrl: 'view/profile.html', 
      parent: angular.element(document.body), 
      clickOutsideToClose:true 

     }) 
    } 

我想TP簡檔傳遞給ProfileController可顯示和配置文件數據。在簡檔控制器,我得到的數據,因爲這

function profileController($scope,..., profileId){ 

} 

但在控制檯

Error: [$injector:unpr] Unknown provider: profileIdProvider <- profileId<- ProfileController 

這是什麼錯誤,以及如何解決它這個錯誤apear?

回答

6

我加在配置文件模板ng-controller="ProfileController as profileController"數據,這是由於錯誤。通過刪除它,我的問題解決了。

0

我認爲你必須這樣做:

controller: ['$scope', function($scope) { 
       var self = this; 
       self.profileId= $scope.profileId; 
      }] 

你簡檔是在範圍之內。

您可以使用當地人來傳遞數據:從官方網站 例:

function showDialog($event) { 
     var parentEl = angular.element(document.body); 
     $mdDialog.show({ 
     parent: parentEl, 
     targetEvent: $event, 
     template: 
      '<md-dialog aria-label="List dialog">' + 
      ' <md-dialog-content>'+ 
      ' <md-list>'+ 
      '  <md-list-item ng-repeat="item in items">'+ 
      '  <p>Number {{item}}</p>' + 
      '  '+ 
      ' </md-list-item></md-list>'+ 
      ' </md-dialog-content>' + 
      ' <md-dialog-actions>' + 
      ' <md-button ng-click="closeDialog()" class="md-primary">' + 
      '  Close Dialog' + 
      ' </md-button>' + 
      ' </md-dialog-actions>' + 
      '</md-dialog>', 
     locals: { 
      items: $scope.items 
     }, 
     controller: DialogController 
     }); 

如果項目是傳遞給對話框

+0

沒有真正回答的問題,我想保持代碼中分離出來也不好把所有的代碼在1頁,難以遵循和維護。 –

0

走快路!

openDialog = (items) => 
    $mdDialog.show({ 
     templateUrl: 'view/profile.html', 
     controller: $scope => $scope.items = items 
    }) 

$scope.items現在可以在對話框模板中使用☺