如何在 angular material 中将数据传递给 $mdDialog
How pass data to $mdDialog in 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
这是什么错误以及如何解决?
我认为你必须这样做:
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 是传递给对话框的数据
我在配置文件模板中添加了 ng-controller="ProfileController as profileController"
,这是由于错误所致。通过删除它我的问题解决了。
走快线!
openDialog = (items) =>
$mdDialog.show({
templateUrl: 'view/profile.html',
controller: $scope => $scope.items = items
})
$scope.items
现在可以在对话框模板中使用 ☺
我想将一些数据传递给 $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
这是什么错误以及如何解决?
我认为你必须这样做:
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 是传递给对话框的数据
我在配置文件模板中添加了 ng-controller="ProfileController as profileController"
,这是由于错误所致。通过删除它我的问题解决了。
走快线!
openDialog = (items) =>
$mdDialog.show({
templateUrl: 'view/profile.html',
controller: $scope => $scope.items = items
})
$scope.items
现在可以在对话框模板中使用 ☺