ching.lin
ching.lin

Reputation: 13

AngularJS ng-click error when click second times

I use ng-repeat and ng-click at span tag. It generate ten span tag and each span tag has one value. I want to click the span and get the value. When I click one span, I get the value. But when I click another span, it get error. Anyone can help me?

error code:

TypeError: l is not a function
    at angular.js:179
    at f (angular.js:196)
    at a.$$childScopeClass.$$childScopeClass.$eval (angular.js:114)
    at a.$$childScopeClass.$$childScopeClass.$apply (angular.js:114)
    at HTMLSpanElement.<anonymous> (angular.js:196)
    at HTMLSpanElement.dispatch (jquery.min.js:3)
    at HTMLSpanElement.r.handle (jquery.min.js:3)
(anonymous) @ angular.js:93
(anonymous) @ angular.js:69
$apply @ angular.js:114
(anonymous) @ angular.js:196
dispatch @ jquery.min.js:3
r.handle @ jquery.min.js:3

html code:

<div class="swiper-slide" ng-repeat="n in [] | range:10">
    <span ng-click="selectValue($index+1)">{{$index+1}}</span>
</div>

Controller:

var myApp = angular.module('myApp', ['ngCookies']);

myApp.filter('range', function () {
    return function (input, total) {
    total = parseInt(total);

    for (var i = 0; i < total; i++) {
         input.push(i);
    }
    return input;
   };
});
function selectController($scope, $http, $cookieStore) {
    $scope.selectValue = function (value) {
        $scope.selectValue = value;
        $cookieStore.put("selectValue", $scope.selectValue);
        console.log("selectValue" + $scope.selectValue);
        console.log($cookieStore.get("selectValue"));
    }
}

Upvotes: 0

Views: 327

Answers (1)

Kaustubh Khare
Kaustubh Khare

Reputation: 3510

You are using the same variable for onClick function and to assign selected value. Change your $scope.selectValue variable by $scope.selectedValue.

You have the same variable that's why after the first click function is replaced by value and when you are trying to click the second time, it could not find function.

$scope.selectValue = function (value) {
        $scope.selectedValue = value;
        $cookieStore.put("selectValue", $scope.selectedValue);
        console.log("selectValue" + $scope.selectedValue);
        console.log($cookieStore.get("selectValue"));
    }

Upvotes: 1

Related Questions