如何在Angular Material中将数据传递给$mdDialog

7

我想把一些数据传递给 $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

        })
    }

我希望能够传递profileId到profileController,并显示相关的profile数据。在profileController中,我获取数据的方式如下:

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

}

但是这个错误会在控制台中出现。
  Error: [$injector:unpr] Unknown provider: profileIdProvider <- profileId<- ProfileController

这是什么错误,如何修复它?
3个回答

8

我在个人资料模板中添加了ng-controller="ProfileController as profileController",但出现了错误。删除它后,问题得到解决。


1
我认为你必须这样做:

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

您的profileId在范围内。

您可以使用locals传递数据: 来自官方网站的示例:

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
      });

items是传递给对话框的数据


并不是问题的答案,我想要保持代码分离,把所有代码放在一个页面上不好跟踪和维护。 - Al-Mothafar

0
走捷径吧!
openDialog = (items) => 
    $mdDialog.show({
        templateUrl: 'view/profile.html',
        controller: $scope => $scope.items = items
    })

$scope.items现在可以在对话框模板中使用☺


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接