2015-04-23 47 views
0

我试图在用户关闭模式时使用console.log表单字段,但我不确定如何执行此操作。在Angular中关闭模式时记录表单字段值

当我使用下面的代码时,表单字段中的输入未被反映。有任何想法吗?

的Javascript:

.controller('ContactFormCtrl', 
function($modal) { 
    var contactForm = this; 
    contactForm.agreement = agreement; 
    contactForm.contact.signature = ''; 

return; 

function agreement() { 

$modal.open({ 

    templateUrl: 'views/agreement.html' 
    }) 
    .result.then(
    function() { 
     var agreement=contactForm.contact.signature; 
     console.log(agreement); 
     (contactForm.value1 = true); 

    }, 
    function() { 
     contactForm.value1 = false; 
    } 
); 
} 
}); 

HTML:

<form name="paymentForm"> 
    <div class="form-group> 
    <label class="control-label" for="signature">Signature</label> 
    <input type="text" id="signature" name="signature" ng-model="contactForm.contact.signature" aria-describedby="signatureWarning" placeholder="Signature (e.g., /John Doe/)" class="form-control" ng-minlength=1 ng-model-options="{ updateOn: 'default blur', debounce: {'default': 500, 'blur': 0} }" required /> 
</div> 

<button ng-click="$dismiss()" class="btn btn-warning">Cancel</button> 
<button ng-click="$close()" class="btn btn-primary" ng-disabled="paymentForm.$invalid">I Agree</button> 

回答

0

你必须更轻松地处理模态控制器内的表单数据比你提取它。从$modal以外访问示波器并不是一个很好的/推荐的方法,特别是因为模态本身有几个示波器层。

这将是最好有一个save方法或东西:

$scope.save = function() { 
    // POST data somewhere, save to a service, whatever 
    $modalInstance.$close(); // or whatever the API is 
} 

<button ng-click="save()" class="btn btn-primary" ng-disabled="paymentForm.$invalid">I Agree</button> 
+0

感谢您的答复。那么是不是可以通过我现在使用的代码在模态的输入字段中传递文本? – Ken

+0

如果您在控制器*内处理*,可以“传递”这些值。尝试从外部组件访问这些值将会很困难,而且不是安全的方法。你会潜入孩子的范围,等等你想通过他们?您可以发出xhr请求与服务器通话,使用服务共享数据,甚至触发事件。 – helion3

+0

我想我可能自己想出了这个。我的目标是要有一个模式,要求用户签署他/她的名字来同意服务条款。当模式关闭时,我想要保留该信息,直到表单的其余部分完成。提交表单时,我希望模型中的信息签名信息也可以提交。所以“/ John Doe /”将与表格的其余部分一起提交。 – Ken

相关问题