Rakesh Lochan Sarma Rakesh Lochan Sarma - 1 year ago 108
AngularJS Question

angular.js:11706 TypeError: k is not a function

I am using a Modal in my app. This is the piece of code in the modal:

<div class="btn-group" ng-repeat='r in range' >
<button type="button" class="btn btn-default" style="width:40px"
ng-click="score(r);" >{{r}}</button>

Below is the function:


The problem I am facing is, if I click once on a button it exactly prints the result in console. But after that if I try once more clicking on another button then I get the below error:

angular.js:11706 TypeError: k is not a function

But if I close the modal and only once it does not through the error. I am really confused. Is it because of the hoisting issue? Please give me a solution if anyone has.

Example Code:

function TodoCtrl($scope) {
$scope.range = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
$scope.score = function(scr) {
$scope.scored = true
$scope.score = scr;

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
<div ng-controller="TodoCtrl">
<div class="btn-group" ng-repeat='r in range'>
<button type="button" class="btn btn-default" style="width:40px" ng-click="score(r);">{{r}}</button>

Answer Source

The first time you click the button, it runs the function. Part of the function is this line: $scope.score=scr;. This reassigns what used to be the function to instead be whatever was passed to it as its argument. The second time you click the button it tries to run the new value of $scope.score, but that new value isn't a function so it gives you the error you see.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download