2016-01-13 100 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现在可以在对话框模板中使用☺