ng-click仅在ng-repeat内部工作一次

时间:2016-03-16 16:10:14

标签: javascript angularjs

我是Angular的新手,在ng-click内与ng-repeat挣扎。出于某种原因,它只会触发一次,并且以下几次单击会抛出以下错误:

enter image description here

控制器:

(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>

3 个答案:

答案 0 :(得分:1)

在此代码中,您将使用单击的潜在客户替换函数activateLead:

$scope.activateLead = lead

不确定这条线应该做什么,但这是问题所在:

{{1}}

答案 1 :(得分:0)

验证功能:

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

变量名称错误!

答案 2 :(得分:0)

控制器:

  $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>