AngularJS Material mdDialog 并在模板中显示局部变量
AngularJS Material mdDialog and display locals in a template
生成 mdDialog
的部分控制器看起来像
$scope.removeAttendee = function(item) {
console.log(item);
$mdDialog.show({
controller: DialogController,
templateUrl: 'views/removeMsg.tmpl.html',
parent: angular.element(document.body),
clickOutsideToClose:true,
controllerAs: 'ctrl',
fullscreen: $scope.customFullscreen, // Only for -xs, -sm breakpoints.
locals: {item: item}
})
和 mdDialog
的控制器:
function DialogController($scope, $mdDialog) {
var attendee = this;
$scope.hide = function() {
$mdDialog.hide();
};
$scope.cancel = function() {
$mdDialog.cancel();
};
$scope.save = function(response) {
$mdDialog.hide(response);
};
$scope.remove = function(response) {
$mdDialog.hide(response);
};
}
removeMsg.tmpl.html
有代码
<p>You are going to remove {{ ctrl.item.firstName }} {{ ctrl.item.lastName }} from the lunch list.</p>
但我无法显示相关值,即使我将代码更改为
之类的简单代码
locals { item: "test" }
和
的相关部分
{{ ctrl.item }}
关于为什么我没有显示这些值的任何建议?
由于您使用 DialogController
和 controllerAs
别名,您应该将已解析的值分配给控制器上下文 item
变量
function DialogController($scope,$mdDialog,item){//Item value will resolve via locals
var attendee = this;
attendee.item = item; // this line is important.
$scope.hide = function() {
$mdDialog.hide();
};
$scope.cancel = function() {
$mdDialog.cancel();
};
$scope.save = function(response) {
$mdDialog.hide(response);
};
$scope.remove = function(response) {
$mdDialog.hide(response);
};
}
除此之外,请将 $scope
方法转换为使用 attendee
(this)。由于 controllerAs
模式不鼓励在控制器中使用 $scope
。
生成 mdDialog
的部分控制器看起来像
$scope.removeAttendee = function(item) {
console.log(item);
$mdDialog.show({
controller: DialogController,
templateUrl: 'views/removeMsg.tmpl.html',
parent: angular.element(document.body),
clickOutsideToClose:true,
controllerAs: 'ctrl',
fullscreen: $scope.customFullscreen, // Only for -xs, -sm breakpoints.
locals: {item: item}
})
和 mdDialog
的控制器:
function DialogController($scope, $mdDialog) {
var attendee = this;
$scope.hide = function() {
$mdDialog.hide();
};
$scope.cancel = function() {
$mdDialog.cancel();
};
$scope.save = function(response) {
$mdDialog.hide(response);
};
$scope.remove = function(response) {
$mdDialog.hide(response);
};
}
removeMsg.tmpl.html
有代码
<p>You are going to remove {{ ctrl.item.firstName }} {{ ctrl.item.lastName }} from the lunch list.</p>
但我无法显示相关值,即使我将代码更改为
之类的简单代码locals { item: "test" }
和
的相关部分{{ ctrl.item }}
关于为什么我没有显示这些值的任何建议?
由于您使用 DialogController
和 controllerAs
别名,您应该将已解析的值分配给控制器上下文 item
变量
function DialogController($scope,$mdDialog,item){//Item value will resolve via locals
var attendee = this;
attendee.item = item; // this line is important.
$scope.hide = function() {
$mdDialog.hide();
};
$scope.cancel = function() {
$mdDialog.cancel();
};
$scope.save = function(response) {
$mdDialog.hide(response);
};
$scope.remove = function(response) {
$mdDialog.hide(response);
};
}
除此之外,请将 $scope
方法转换为使用 attendee
(this)。由于 controllerAs
模式不鼓励在控制器中使用 $scope
。