javascript - Form validity using Angular ng-repeat -
i have angular form parsing json
data.
i'm rendering using ng-repeat
. however, i'm having issue in form never becomes valid when radio button
in each group selected.
i suspect issue lies ng-model
in each input, can't seem figure out correct way dynamically create ng-model
inside ng-repeat
.
form block code:
<form name="posttestform"> <alert ng-repeat="alert in alerts" type="{{alert.type}}" close="closealert($index)">{{alert.msg}}</alert> <div class="well question-well" ng-repeat="question in questions"> <p> <strong>{{question.question}}</strong> </p> <ul> <li ng-repeat="answers in question.answers"> <input ng-model="q[question.id]" type="radio" name="q{{question.id}}" value="{{answers.id}}" required="" data-key="{{answers.iscorrect}}" />{{answers.answer}} </li> </ul> </div> <button type="submit" class="btn btn-primary" ng-click="evaluateposttest(3)" ng-show="!testpassed"> submit questions</button> </form>
here's plunkr shows dynamic code , demonstrates form never turning valid when radio button in each group selected.
http://plnkr.co/edit/hqgpiocdn3tgle96cpk5?p=preview
and here's plunkr using static content displaying working.
just add in javascript controller
$scope.q = [undefined,undefined,undefined,undefined,undefined,undefined];
explanation : set ng-model q[question.id] q undefined ng-model won't ever work. must initialize variable in controller. case works not initialize when : ng-model="test"
if
ng-model="test.test" ng-model="test[0]"
it won't ever work if it's not initialized properly.
Comments
Post a Comment