使用angular.js獲取form表單中的信息


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注冊</title>
    <script src="./node_modules/angular/angular.js"></script>
</head>
<body ng-app="s1.app">
<div>
    <div>
        <label for="ipt_name">用戶名</label>
        <input ng-model="data.name" type="text" id="ipt_name">
        <span ng-bind="data.errorMsg.name"></span>
    </div>
    <div>
        <label for="ipt_password">密碼</label>
        <input ng-model="data.password" type="password" id="ipt_password">
    </div>
    <div>
        <label for="ipt_age">年齡</label>
        <input ng-model="data.age" type="number" id="ipt_age">
    </div>
    <div>
        <label for="ipt_phone">手機</label>
        <input ng-model="data.phone" type="tel" id="ipt_phone">
    </div>
    <div>
        <button ng-click="actions.submit()">注冊</button>
    </div>
</div>
<script>
    // app是application的縮寫,application是應用程序的意思
    var app = angular.module('s1.app', []);
    app.run(function ($rootScope) {
        // data是數據的意思
        var data = $rootScope.data = {};
        data.name = '';
        data.password = '';
        data.age = 0;
        data.phone = '';
        data.errorMsg = {};

        // actions是行為的意思
        var actions = $rootScope.actions = {};
        actions.submit = function () {
            var userinfo = {};
            if(data.name == ''){
                data.errorMsg.name = '用戶名不能為空';
                return;
            }
            userinfo.name = data.name;
            userinfo.password = data.password;
            userinfo.age = data.age;
            userinfo.phone = data.phone;
            // 從數據對象上拿到我們想要的數據,然后做提交(現在我們沒有服務器,只是log一下)
            console.log(userinfo);
        }
    })

</script>
</body>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM