我可以访问形式的控制? [英] Can I access a form in the controller?

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

问题描述

我目前使用下面的内容。

I'm currently using the following.

$范围$$ childHead.customerForm [名字] ,使

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

但是,这仅适用于Chrome浏览器。现在,我尝试了以下内容:

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

$ scope.editCustomerForm [名字] ,使

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

不工作。请注意我的形式为基础标签内。如何访问的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>

然后,您可以访问的FormController在code,如:

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天全站免登陆