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>
ng-repeat="things in
and then usething
without thes
. 2. You are using it outside of theng-repeat
's scope, which ends with theli
. – Omri Aharon Apr 8 '15 at 12:37