我试图从模态控制器(Plunkr)访问表单,但myForm似乎无法访问。如何让提醒呼叫正常工作:
angular.module('plunker', ['ui.bootstrap']);
var ModalDemoCtrl = function ($scope, $modal, $log) {
$scope.open = function () {
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: ModalInstanceCtrl
});
};
};
var ModalInstanceCtrl = function ($scope, $modalInstance) {
$scope.submit = function() {
// How to get this?
alert($scope.myForm.$dirty);
};
$scope.ok = function () {
$modalInstance.close();
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
};
和模板:
<div ng-controller="ModalDemoCtrl">
<script type="text/ng-template" id="myModalContent.html">
<div class="modal-header">
<h3>I'm a modal!</h3>
</div>
<div class="modal-body">
<form name="myForm" novalidate>
<input type="email" value="hello">
</form>
</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="submit()">OK</button>
<button class="btn btn-warning" ng-click="cancel()">Cancel</button>
</div>
</script>
<button class="btn" ng-click="open()">Open me!</button>
<div ng-show="selected">Selection from a modal: {{ selected }}</div>
</div>
发布于 2014-04-13 23:59:04
Angular-UI modals使用转译来附加modal内容,这意味着在modal中创建的任何新范围条目都会在子范围中创建。这发生在form指令中。
您可以尝试使用(Angular 1.2.16)将表单附加到父作用域:
<form name="$parent.userForm">
在modal的控制器$scope
中创建并提供userForm
。多亏了作用域继承,userForm
访问在标记中保持不变。
<div ng-class="{'has-error': userForm.email.$invalid}"}>
发布于 2014-06-27 21:10:20
在控制器顶部添加:
$scope.form = {};
在html模板中:
<form name="form.yourFormName">
对html元素进行验证:
<div ng-class="{'has-error': form.yourFormName.email.$invalid}"}>
另请参阅:
AngularJS modal dialog form object is undefined in controller
发布于 2014-08-16 06:14:42
最后,我采用了gertas给出的答案,但以下是解决此问题的另一种方法。
//place a method on modal controller scope
$scope.setFormReference = function (myForm) {
$scope.myForm = myForm
};
//call this using an angular expression in your modal template
{{ setFormReference(loginForm)}}
https://stackoverflow.com/questions/20713633
复制相似问题