如何将数据传递给角度材质中的$ mdDialog

时间:2016-01-13 08:57:03

标签: javascript angularjs angular-material

我想将一些数据传递给$ 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将profileId传递给profileController并显示配置文件数据。在配置文件控制器中,我获取数据

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

}

但在控制台

中出现此错误
  Error: [$injector:unpr] Unknown provider: profileIdProvider <- profileId<- ProfileController

这个错误是什么以及如何解决?

3 个答案:

答案 0 :(得分:7)

我在个人资料模板中添加了ng-controller="ProfileController as profileController",这是由于错误造成的。删除它我的问题解决了。

答案 1 :(得分:0)

我认为你必须这样做:

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

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

其中items是传递给对话框的数据

答案 2 :(得分:0)

走捷径!

openDialog = (items) => 
    $mdDialog.show({
        templateUrl: 'view/profile.html',
        controller: $scope => $scope.items = items
    })
现在可以在对话框模板☺

中使用

$scope.items

相关问题