Angular表单提交问题

时间:2016-07-26 11:44:18

标签: angularjs ionic-framework

这是我的signup.html文件,我只想从此表单中获取密码和用户名到我的控制器。但我无法通过此访问表单元素!

<ion-view view-title="Sign Up">
        <ion-content ng-controller="signup">
            <div class="figure_img">
                <img src="img/logo_image.png">
            </div>
            <form name="myForm">
                <div class="list">
                    <label class="item item-input">
                        <input type="text" placeholder="Email ID" id="email" name="email" ng-model="formData.email"
                               required>
                        <span ng-show="myForm.email.$error.required && myForm.email.$dirty">required</span>
                        <span ng-show="!myForm.email.$error.required && myForm.email.$error.email && myForm.email.$dirty">invalid email</span>
                    </label>
                    <label class="item item-input">
                        <input type="password" placeholder="Password" id="password" name="password"
                               ng-model="formData.password" required>
                        <span ng-show="myForm.password.$error.required && myForm.password.$dirty">required</span>
                    </label>


                    <label class="item item-input">
                        <input type="password" placeholder="Confirm Password" id="password_c" name="password_c" ng-model="formData.password_c" valid-password-c required>
                        <span ng-show="myForm.password_c.$error.required && myForm.password_c.$dirty">Please confirm your password.</span>
                        <span ng-show="!myForm.password_c.$error.required && myForm.password_c.$error.noMatch && myForm.password.$dirty">Passwords do not match.</span>

                        </label>

                    <div class="contain-body">
                        <button class="button button-block button-positive" ng-click="signup(myForm)">SignUp</button>
                    </div>
                </div>
            </form>
        </ion-content>
    </ion-view>

这是我的注册控制器

app.controller("signup", function ($scope,$http,$ionicPopup,URL,$ionicLoading,$timeout,$state) {
    $scope.data = {
        email :'',
        password :''
    };

    $scope.signup = function (data) {
        console.log(data);
        $http.post(URL.url + '/registration',data)
            .success(function () {
                $ionicLoading.show({template:'<ion-spinner class="spinner-energized"></ion-spinner>Complete your Registration through Email'});
                $timeout(function () {
                    $ionicLoading.hide();
                    $ionicHistory.clearCache().then(function() {
                        $state.go('EmailLogin') ;
                    });
                },5000);

            }).error(function (response) {
            $ionicPopup.alert({
                title: 'Sign-Up failed!',
                template:response.errors.email
            });
        });

    }
});

我怎么能在我的控制器中只使用密码和用户名.. 这样就可以轻松注册了!

2 个答案:

答案 0 :(得分:1)

您实际上是将表单控制器传递给注册方法。这不是你想要的。

您已经可以通过视图中的passwordformData.email访问您的formData.passwordsignup字段值,因此您可以调整对$scope.data的调用通过在评论中建议包含formData。

但是当您声明ng-model是您的控制器时,您可能希望在视图的data指令中重用那些而不是formData,因为当前这个struct Player { let name : String var cards : [Card] = [] init(name: String) { self.name = name } } 对象似乎不是使用。

答案 1 :(得分:0)

使用您收到的数据在函数中创建一个对象。只需执行var reqData = { email: data.email, password: data.password };之类的操作,然后执行$http.post(URL.url + '/registration',reqData)

希望它有所帮助。