在角形表单上执行ng-click AND ng-switch提交

时间:2014-02-18 13:56:37

标签: forms angularjs

我有一个单页角应用程序。着陆页是一个登录表单,其中包含指向注册表单的链接。在用户通过身份验证之前,应用程序的其余部分不可见。我使用ng-switch指令交换登录和注册页面。因此,当加载登录表单并且用户单击“注册”按钮时,ng-switch指令会对signin部分的注册部分进行切换。我需要执行“createAccount()”函数并在提交注册表单时将ng-switch切换回signin部分。这可能吗?现在,当我点击提交时,它只是执行ng-switch并忽略createAccount()位。

<body ng-controller="MainCtrl">
<div ng-switch on="view.controller">

    <div ng-cloak class="ng-cloak" ng-switch-default ng-controller="UserCtrl">

        <div ng-show="!isAuthenticated">
            <form class = "form-signin">

                <div class="control-group">

                    <div class="controls">
                        <br />
                        <input type="text" ng-model="username" placeholder = "Email Address" > 
                    </div>

                    <br />
                    <div class="controls">
                      <input type="password" ng-model="password" placeholder = "Password" >
                    </div>
                    <div class="controls">
                        <button class = "btn btn-primary" ng-click="signIn()">Sign In</button>


                        <button class = "btn btn-default" ng-click= "view.controller = 'AccountNewCtrl'" >Register</button>

                    </div> 
                </div>
            </form>
        </div>


<!--Start of sign up form-->

    <div ng-switch-when="AccountNewCtrl" >
        <div ng-controller = "AccountNewCtrl" >
            <div ng-cloak class="ng-cloak" ng-show="!isAuthenticated">

                <div class="page-header">
                    <h1>Sign Up<br />

                </div>
                <div class="col-xs-6">

                    <form id = "myForm" name="myForm" ng-submit="createAccount()"> 

//Form input code here      


//On clicking this button, the createAccount() function should happen and then the     ng-click event should happen 
                        <button type="submit" ng-click= "view.controller = 'UserCtrl'" class="btn btn-small btn-primary">Register</button>

                    </form>
            </div>    
        </div>

1 个答案:

答案 0 :(得分:1)

在你的createAccount()函数中,你可以做到

$scope.$parent.view.controller = 'UserCtrl';

这样,您就不需要在注册表单上的提交按钮上添加ng-click

相关问题