简体   繁体   中英

ng-click works only once inside ng-repeat

I am new to Angular and struggling with ng-click inside ng-repeat . For some reason, it only fires once, and the following clicks throw the following error:

在此处输入图片说明

Controller:

(function ( window, angular ) {
    'use strict'

    var LeadsController = function ( $scope, $http, $cookies, breadcrumbFactory ) {
        $scope.leads = []
        $scope.activeLead = undefined
        $scope.breadcrumb = breadcrumbFactory

        $scope.getLeads = function () {
            $http({
                method: 'GET',
                url: '/admin/leads'
            })

            .success( function getLeadsSuccess ( data ) {
                $scope.leads = data.data
                $scope.activeLead = $scope.leads[0]
            })

            .error( function getLeadsError ( err ) {
                console.error( err )
            })
        }

        $scope.activateLead = function () {
            $scope.activeLead = this.lead
        }

        $scope.getLeads()
    }

    angular
        .module( 'app' )
        .controller( 'LeadsController', [
            '$scope',
            '$http',
            '$cookies',
            'breadcrumbFactory',
            LeadsController
        ])
})( window, angular );

HTML:

<ul class="list list--dual-line" id="leads-list">
    <li ng-repeat="lead in leads">
        <a ng-click="activateLead()">
            <p>{{ lead.firstName }} {{ lead.lastName }}</p>
            <small>{{ lead.email }}</small>
        </a>
    </li>
</ul>

In this code you're replacing the function activateLead with the lead that's being clicked:

$scope.activateLead = function ( lead ) {
        debugger
        $scope.activateLead = lead
    }

Not sure what this line is supposed to do but this is the problem:

$scope.activateLead = lead

Verify function:

$scope.activateLead = function ( lead ) {
    debugger
    $scope.activeLead = lead
}

the variable name is wrong!

Controller:

  $scope.activateLead = function(lead) {
    $scope.activeLead = lead;
  };

HTML:

  <ul class="list list--dual-line" id="leads-list">
    <li ng-repeat="lead in leads">
      <a ng-click="activateLead(lead)">
        <p>{{ lead.firstName }} {{ lead.lastName }}</p>
        <small>{{ lead.email }}</small>
      </a>
    </li>
  </ul>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM