1

This app allows user to enter the quantity of questions for specific topic inside a workbook. I’m trying to sum up all the questions and give an output of totalQuestions.

I created a function (findTotalQuestions().) that will run when Quantity input box is changed. I’m getting an error in Console: Cannot read property 'quantity' of undefined. I’m thinking its because $scope.skill.quantity is not being passed into function. How can I fix this?

<div class="input-field col m3" ng-model="totalQuestions">
   <medium>Total Questions in Workbook: </medium>{{totalQuestions}}
</div>

HTML

<table>
     <tr>
        <th> Skill </th>  
        <th> Quantity </th>
     </tr>
     <tr ng-repeat="skill in newWorkbook.skills">
         <td>
            { skill.topicText }}
         </td>
         <td>
              <p>
                <input id="quantity{{ skill.TopicId }}" type="number" class="validate" ng-model="skill.quantity" required="true" autocomplete="off" ng-change="findTotalQuestions()">
              </p>
         </td>
       </tr>
 </table>

Controller:

$scope.getProposedSkills = function() {
  return $http.get("/api/topics?SubjectId=" + $scope.newWorkbook.SubjectId).then(function(skills) {
    return $scope.newWorkbookskills = _.map(skills.data, function(skill) {
      var obj;
      obj = {
        TopicId: skill.id,
        quantity: 0,
        level: null,
        topicText: skill.topicText,
        startLevel: skill.startLevel,
        endLevel: skill.endLevel
      };
      return obj;
    });
  });
};

$scope.findTotalQuestions = function() {
  $scope.totalQuestions = 0;
  $scope.totalQuestions = $scope.totalQuestions + $scope.skill.quantity;
  return console.log($scope.totalQuestions);
};

2 Answers 2

2

Move your getProposedSkills() code in a service then in the controller set the $scope.skill like below

app.service('service',function($http){
    return {getProposedSkills:$http.get('your api url')};
})

Your controller should look like below

app.controller('ctrl',function($scope,$http,service){
   $scope.getProposedSkills = service.getProposedSkills().then(function(res){
       $scope.skill=res;
   })      

  $scope.findTotalQuestions = function() {
     $scope.totalQuestions = 0;
     $scope.totalQuestions = $scope.totalQuestions + $scope.skill.quantity;
     return console.log($scope.totalQuestions);
  }
})
Sign up to request clarification or add additional context in comments.

Comments

1

And other question i can't find the place where you set $scope.skill in your controller, if it's a paremeter to be used in your funcion, you should receive it as a paremter of your function and pass it form the view if it's a variable on your controller it should be initialized or at least check if it exists.

By the way, In the html you have a div with ng-model, this is not correct ng-model should be place inside form inputs (select, input, textarea)

1 Comment

$scope.skill was determined in the controller, i didn't include it in the question. I wasn't sure if it was necessary to include it. thanks for the advice. i will update my ng-model.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.