angular.js - ionic gets the value of undefined input ng-model?
滿天的星座
滿天的星座 2017-05-15 17:07:34
0
1
524

Scene:

问卷调查类项目,问卷后台提供,前端展示并提交表单

Question:

因为题型以及数量的不确定,再提交的时候如何获取每个问题答案的ng-model的值

Supplement:

希望尽量在ionic或angular范围内解决,但不强求


感谢!
滿天的星座
滿天的星座

reply all(1)
伊谢尔伦
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body ng-app="myApp">
    <p ng-controller="myController">
        
        <p ng-repeat="ti in tiList">
            
            <input type="text" ng-model="data" ng-value="ti.result" ng-change="change($index,ti.name,data)">    
        </p>
        <button ng-click="submit()">提交</button>
    </p>
    <script src="lib/js/angular.js"></script>
    <script>
        angular.module('myApp',[])
                .controller("myController",["$scope",function($scope){
                    
                    $scope.tiList = [
                        {"name":"t1","result":""},
                        {"name":"t2","result":""},
                        {"name":"t3","result":""},
                        ];
                    var tiArr = [];
                    $scope.change = function(index,tiName,data){
                        for(var i=0;i<tiArr.length;i++){
                            //判断是否再次修改
                            if(tiArr[i].tiName == tiName){
                                tiArr[i].tiResult = data;
                                console.log(index);
                                return;    
                            }
                            
                        }

                        console.log(tiName,data);

                        var tiObj = {"tiName":tiName,
                                     "tiResult":data};

                        console.log(tiObj);
                        tiArr.push(tiObj);
                    }
                    //提交
                    $scope.submit =function(){
                        console.log(tiArr);

                    }

                }])
    </script>
    
</body>
</html>

之前考虑的不周全,重写了一版。还有一点bug,就是所有的input的得从上往下依次写
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template