Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm working on a backend for a personality quiz where the user can send inn names for the different personalities, some questions, and alternative answers to the questions. The user will (hopefully) also be able to set up how many points each character shall gather from the different alternatives of an answer. There's some arrays inside of arrays involved, and objects in arrays. My problem appear when setting up the form for sending in the different points for the alternaitive answers. I get a "Cannot read property of undefined" when I try to log the points to the console. Heres the code:

<html lang="en" ng-app="backend">
<head>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head>
<body>
    <h1>points test</h1>      
    <div ng-controller="QuestionsController">
        <ul class="paa-linje">
          <form novalidate>
            <li class="paa-linje" ng-repeat="things in pointsArr track by $index">
             <input class="input"  type="number" min="0" max="10" ng-model="thing.points" required placeholder="0"/>
             </li>
            <input type="submit" ng-click="personPoints(thing)" value="Submit">
          </form>
        </ul>
  </div>
  <script>
      var app = angular.module('backend', [])

      app.controller('QuestionsController', function($scope) {
          $scope.pointsArr = [
          {nbr: 0, points: ''},
          {nbr: 1, points: ''},
          {nbr: 2, points: ''}
          ];

          $scope.personPoints = function(field) {
              console.log("points = " + field.points);
              for (var i = 0; i < $scope.pointsArr.length; i++) {
                  console.log("points = " + field.points);
              };
          };
      });
</script>

share|improve this question
4  
2 issues: 1. you write ng-repeat="things in and then use thing without the s. 2. You are using it outside of the ng-repeat's scope, which ends with the li. – Omri Aharon Apr 8 '15 at 12:37
2  
2 more issues: 1. you nest a form directly after a list. 2. You are using a submit button inside a repeat (which would submit the whole form. These -probably- won't break anything, it's just terrible HTML. – Mike Robinson Apr 8 '15 at 12:39
    
Thanks. Sorry for the bad HTML. What I really want to do is send in information from all three fields with one click. How do I do that? – SigmundO Apr 9 '15 at 10:59
    
I tried putting the form tag outside the list and using ng-submit inside the form tag: <form novalidate ng-submit="personPoints(things)">. And: </ul> <input type="submit" value="Submit"> </form> – SigmundO Apr 9 '15 at 11:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.