关闭MD对话框不工作后将项目推送到数组
问题描述:
通过MD对话框推送项目后,数组不会刷新,但我可以正常保存该项目。关闭MD对话框不工作后将项目推送到数组
我试图测试我可以手动推哪一级的项到阵列,我可以这样做,直到$scope.showAddUsuario()
,之后,我无法推送的项目,甚至手动:
Usuario.html
<div flex-gt-sm="100" flex-gt-md="100" ng-controller="UsuarioCtrl">
<h2 class="md-title inset">Usuario</h2>
<md-card>
<md-list>
...
</md-list>
</md-card>
<md-button class="md-fab" aria-label="Add" ng-click="showAddUsuario($event)">
<md-icon md-svg-icon="content:ic_add_24px" aria-label="Plus"></md-icon>
</md-button>
</div>
MD-对话框:
<md-dialog aria-label="Form">
<md-content class="md-padding">
<form name="userForm">
<div layout layout-sm="column">
<md-input-container flex> <label>Nome</label> <input ng-model="item.nome"> </md-input-container>
</div>
<div layout layout-sm="column">
<md-input-container flex> <label>E-mail</label> <input ng-model="item.email"> </md-input-container>
</div>
<div layout layout-sm="column">
<md-input-container flex> <label>Senha</label> <input ng-model="item.senha"> </md-input-container>
</div>
</form>
</md-content>
<div class="md-actions" layout="row">
<span flex></span>
<md-button ng-click="cancel()"> Cancel </md-button>
<md-button ng-click="saveUsuario(item)" class="md-primary"> Save </md-button>
</div>
</md-dialog>
控制器:
app.controller('UsuarioCtrl', function ($scope, $http, $mdDialog, $interval, $timeout) {
$scope.items = [];
$http({
method : 'GET',
url : 'UsuarioServlet'
})
.success(
function(data, status, headers,
config) {
$scope.items = data;
}).error(
function(data, status, headers,
config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
$scope.saveUsuario = function(item) {
$scope.items.push({id:100, nome:item.nome, email:item.email, senha:item.senha, status:1});
};
$scope.showAddUsuario = function(ev) {
$mdDialog.show({
controller: 'UsuarioCtrl',
templateUrl : 'CrudUsuario.html',
targetEvent : ev,
locals : {
item : null
}
})
};
});
答
现在我才看到你使用$ mdDialog而不是$ modal。所以这个答案很可能不适用于你的情况。
但是您似乎确实在代码中缺少了.finally()
部分。 从说明书(https://material.angularjs.org/latest/api/service/$mdDialog)
$mdDialog
.show(alert)
.finally(function() {
alert = undefined;
});
所以,你可以试试。 否则,你有没有考虑过使用$ modal?
你似乎没有处理你的模态信息的返回。
(function(){
'use strict';
function myCtrl($modal){
var vm = this;
vm.myArray = [];
...
function openModal(){
var modalInstance = $modal.open({
templateUrl: 'path/to/modal/view.html',
controller: 'MyModalCtrl as vm',
size: 'lg',
resolve: {
data: function(){
return dataThatYouWantToSendFromHereToYourModal;
}
}
});
modalInstance.result.then(function (result){
vm.myArray.push(result);
});
}
...
}
...
})();
的modalIntsance.result.then
将触发时,你在你的模态控制器(在这种情况下MyModalCtrl
)发出$modalInstance.close(sendThisDataBackToCallingController)
电话。
因此,模态控制器应沿着
...
function MyModalCtrl($modalInstance, data){
...
function init(){
doSomethingWithDataThatYouGotFromTheCallingController(data);
}
...
function save(){
var dataToSendBack = {...};
$modalInstance.close(dataToSendBack);
}
...
}
...
这应该让你在正确的方向前进的线条看起来。
答
感谢您的意见!该文件说要使用:
})。然后(功能(项目){ $ scope.items.push({id:100,nome:item.nome,电子邮件:item.email,senha:项目。 senha,status:1}); });
工作正常!
答
我解决这个问题,请更换$ mdDialog.show()作为
$mdDialog.show({
controller: function(){
this.parent = $scope;
},
templateUrl: 'dialog1.tmpl.html',
scope:$scope.$new(),
targetEvent : ev,
bindToController: true,
clickOutsideToClose:true,
fullscreen: useFullScreen
})
感谢您的意见!该文档声称使用: })。然后(函数(item){$ scope.items.push({id:100,nome:item.nome,email:item.email,senha:item.senha,状态:1});}); 工作正常! –