[英]Call parent function when click submit inside modal which is the component angularjs
I have a modal which is a component. 我有一个模态,它是一个组成部分。 When I fill form in this modal and click submit I would like to invoke function in parent. 当我在此模式下填写表格并单击提交时,我想在父级中调用函数。
parent controller.js 父controller.js
.module('app.test')
.controller('TestController', function ($uibModal) {
let vm = this
vm.addTest = addTest
vm.openAddTestModal = openAddTestModal
function openAddTestModal() {
$uibModal.open({
component: 'AddTestModalComponent',
windowClass: 'test-modal',
})
}
function addTest(test) {
//do something
}
})
modal.component.js modal.component.js
templateUrl: 'app/modals/add-test-modal.html',
controllerAs: 'vm',
controller: function () {
this.testToSave = ['']
}
})
modal.component.html modal.component.html
<div class="modal-header">
<h2 class="modal-title">Add test</h2>
</div>
<div class="modal-body">
<div>
<form>
<label class="control-label">Test</label>
<input class="form-control" name="" type="text" required="true" ng-model=""/>
</div>
<div class="add-new"><a href="" ng-click="">+ Add test</a></div>
</div>
<div class="mt-4 d-flex justify-content-end">
<button class="btn btn-primary btn-blue" type="submit" ng-click="vm.addTest(vm.test)">Save</button>
</div>
</div>
So if I click Save
I would like to invoke function addTest()
which is inside parent controller. 因此,如果单击“ Save
我想调用父控制器内部的addTest()
函数。 How can I do this? 我怎样才能做到这一点?
The $uibModal.open
returns an object on which the result
property contains a promise that is resolved with the result upon closing the modal or rejected with the reason upon cancelling the modal. $uibModal.open
返回一个对象,其result
属性包含一个promise,该对象在关闭模式时会用结果解析,而在取消模式时会被拒绝。
In modal.component.js 在modal.component.js中
templateUrl: 'app/modals/add-test-modal.html',
controllerAs: 'vm',
controller: function ($modalInstance) {
this.testToSave = [''];
this.addTest = function (result) {
$modalInstance.close(result);
};
}
})
In parent controller.js 在父controller.js中
function openAddTestModal() {
$uibModal.open({
component: 'AddTestModalComponent',
windowClass: 'test-modal',
}).result.then(function(result) {
console.log(result);
vm.addTest(result);
}).catch(function(reason) {
console.log(reason);
throw reason;
});
}
From the Docs: 从文档中:
return 返回
The
open
method returns a modal instance, an object with the following properties:open
方法返回一个模式实例,一个具有以下属性的对象:
close(result)
(Type: function) - Can be used to close a modal, passing a result.close(result)
(Type:function) -可用于关闭模式,并传递结果。
dismiss(reason)
(Type: function) - Can be used to dismiss a modal, passing a reason.dismiss(reason)
(Type:function) -可用于通过原因而消除模态。
result
(Type: promise) - Is resolved when a modal is closed and rejected when a modal is dismissed.result
(类型:promise) -在关闭模态时解决,在取消模态时拒绝。
opened
(Type: promise) - Is resolved when a modal gets opened after downloading content's template and resolving all variables.opened
(类型:promise) -在下载内容的模板并解析所有变量后打开模态时解决。
closed
(Type: promise) - Is resolved when a modal is closed and the animation completes.closed
(类型:promise) -在关闭模式并完成动画时解决。
rendered
(Type: promise) - Is resolved when a modal is rendered.rendered
(Type:promise) -渲染模态时解析。
For more information, see 有关更多信息,请参见
You need to share the scope with the modal like this 您需要像这样与模态共享范围
.module('app.test')
.controller('TestController', function ($uibModal, $scope) {
let vm = this
vm.addTest = addTest
vm.openAddTestModal = openAddTestModal
$scope.addTest = function(test) {
//do something
}
function openAddTestModal() {
$uibModal.open({
component: 'AddTestModalComponent',
scope: $scope,
windowClass: 'test-modal',
})
}
})
Then, in your dialog call it like this 然后,在对话框中像这样调用它
<button class="btn btn-primary btn-blue" type="submit" ng-click="addTest(vm.test)">Save</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.