angularjs - 在其中使用ng-repeat形式提交时出错

时间:2016-01-13 10:42:57

标签: javascript angularjs forms

我有以下形式:两个文本输入,然后是ng-repeat,里面有文本和广播。

<form class="form-horizontal" id="myForm" name="myForm">
        <div class="form-group">
            <label for="name" class="col-sm-2 control-label">Company Name</label>
            <div class="col-sm-10">
                <input type="text" name="cname" ng-model="company.name" class="form-control" required></input>
            </div>
        </div>
        <div class="form-group">
            <label for="category" class="col-sm-2 control-label">HQ</label>
            <div class="col-sm-10">
                <select ng-model="company.hq" ng-options="hq as hq.name for hq in hqs" required>
                <option></option>
                </select>
            </div>
        </div>
        <div class="form-group" ng-repeat="item in getR(4) track by $index">
            <label for="name" class="col-sm-2 control-label">Top pick {{$index+1}}</label>
            <div class="col-sm-10">
                <input type="text" name="quizsize" ng-model="company.product[$index]" class="form-control" required></input>
                <label><input type="radio" name="test" ng-model="company.radioValue" value="{{$index+1}}"/> Choose</label>

            </div>
        </div>
        <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="submit"  value="submit" class="btn btn-primary"  ng-click="addCompany(company)"></button>
            <button type="btn" value="cancel" class="btn btn-danger" ng-click="cancelBtn()">Cancel</button>
        </div>
    </div>
</form>

来自控制器的addCompany方法:

$scope.addCompany = function(company)
    {
        console.log(company.radioValue);
        $http.post('http://localhost/api/index.php/Test/companies', company)
            .success(function(data)
        {
            $scope.companies.push(data[0]);
        })
        .error(function(err)
        {

        })
    };

输入ng-repeat的方法:

$scope.getR = function(n)
    {
        return Array(n);
    }

当我提交时:

如果我从添加公司名称/ hq开始,那么一切都很好,但是 如果我先点击单选按钮开始,那么当我发送表单时,我得到一个未定义的radioValue错误。

1 个答案:

答案 0 :(得分:0)

使用ng-value而不是value来确保模型已准备好绑定到

<input type="radio" ng-value="($index + 1)" ng-model="company.radioValue">

https://docs.angularjs.org/api/ng/directive/ngValue