我是Angular的新手,在ng-click
内与ng-repeat
挣扎。出于某种原因,它只会触发一次,并且以下几次单击会抛出以下错误:
控制器:
(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>
答案 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>