单击第二次时AngularJS ng-click错误

时间:2018-08-23 03:28:31

标签: angularjs

我在span标签上使用ng-repeat和ng-click。它生成十个span标签,每个span标签只有一个值。我想单击范围并获取值。当我单击一个跨度时,我得到了值。但是,当我单击另一个跨度时,会出现错误。有人可以帮助我吗?

错误代码:

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cellType = cellClass[indexPath.item]
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCell", for: indexPath) as! cellType

        cell.addRemoveCellDelegate = self
        cell.label.text = "\(indexPath)"
        switch indexPath.item {
        case 0:
            cell.backgroundColor = .magenta
            cell.screenLabel.text = screens[0]
            case 1:
            cell.backgroundColor = .purple
            cell.screenLabel.text = screens[1]
            case 2:
            cell.backgroundColor = .yellow
            cell.screenLabel.text = screens[2]
            case 3:
            cell.backgroundColor = .green
            cell.screenLabel.text = screens[3]
        default:
            cell.backgroundColor = .blue
        }
        return cell
    }

html代码:

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

控制器:

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

1 个答案:

答案 0 :(得分:1)

您将相同的变量用于onClick函数并分配选定的值。通过$scope.selectValue更改$scope.selectedValue变量。

您具有相同的变量,这就是为什么在将第一次单击的函数替换为值后,当您尝试第二次单击时,找不到函数的原因。

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