我可以访问控制器中的表单吗? [英] Can I access a form in the controller?

查看:27
本文介绍了我可以访问控制器中的表单吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用以下内容.

$scope.$$childHead.customerForm[firstName],这样:

<input type="text" name="firstName"ng-model="data.customer.firstName"标签索引=1"ng-disabled="!data.editable"验证客户/></表单>

但这仅适用于 Chrome.现在我尝试了以下操作:

$scope.editCustomerForm[firstName],这样:

<input type="text" name="firstName"ng-model="data.customer.firstName" tabindex="1"ng-disabled="!data.editable"验证客户/></表单>

这行不通.请注意,我的表单位于 Foundation 选项卡内.我如何访问 firstName?

EDIT:当 form 位于 Foundation 选项卡内时,它看起来没有添加到 scope 中.

有人对此有解决方案吗?

解决方案

虽然在其他评论中提到过,但我想我会为那些使用Controller As"语法的人解释一下:

<表单名称="ctrl.myForm">...输入肮脏的?{{ctrl.myForm.$dirty}}<button ng-click="ctrl.saveChanges()">保存</button></表单>

然后你可以在你的代码中访问 FormController ,如:

function MyController() {var vm = 这个;vm.saveChanges = saveChanges;函数 saveChanges() {如果(vm.myForm.$valid){//保存到数据库或其他.vm.myForm.$setPristine();}}

I'm currently using the following.

$scope.$$childHead.customerForm[firstName], so that:

<form name="customerForm">
  <input type="text" name="firstName" 
         ng-model="data.customer.firstName" 
         tabindex="1"  
         ng-disabled="!data.editable" 
         validationcustomer />
</form>

But this only works in Chrome. Now I tried the following:

$scope.editCustomerForm[firstName], so that:

<form name="customerForm" ng-model="editCustomerForm">
  <input type="text" name="firstName" 
         ng-model="data.customer.firstName" tabindex="1"  
         ng-disabled="!data.editable" 
         validationcustomer />
</form>

Which doesn't work. Note my form is inside a Foundation Tab. How can I access firstName?

EDIT: It looks like the form isn't added to the scope when it's inside a Foundation Tab.

Anyone has got a solution for this?

解决方案

Though alluded to in other comments I thought I'd spell it out a bit for those using the "Controller As" syntax:

<div ng-controller="MyController as ctrl">

<form name="ctrl.myForm">
    ...inputs
    Dirty? {{ctrl.myForm.$dirty}}

    <button ng-click="ctrl.saveChanges()">Save</button>
</form>

</div>

Then you can access the FormController in your code like:

function MyController () {
    var vm = this;
    vm.saveChanges = saveChanges;

    function saveChanges() {

       if(vm.myForm.$valid) { 
            // Save to db or whatever.
            vm.myForm.$setPristine();
       }
}

这篇关于我可以访问控制器中的表单吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆