$scope.Deelnemers
的代码。
示例:我有两个用户/ Deelnemers ID 1和2来自Mysql-db的API。
App.js
getDeelnemers(){
$http({
method: 'POST',
url: 'api.php?users=users',
data: {}
}).then(function successCallback(response) {
$scope.Deelnemers = response.data;
console.log('Deelnemers: ',$scope.Deelnemers);
}, function errorCallback(response) {
alert('Fout met ophalen sponsors!');
});
};
<div class="row" ng-repeat="Deelnemer in Deelnemers">
<div class="col-md-1">
<input name="id" class="form-control input-sm" value="{{Deelnemer.deelnemer_id}}" type="text" disabled>
</div>
<div class="col-md-3">
<input name="names" class="form-control input-sm" value="{{Deelnemer.naam}}" type="text" disabled>
</div>
<div class="col-md-1">
<input name="groupsnr" class="form-control" ng-model="Deelnemer.groupnr" ng-change="saveAssingDeelnemerGroupnr(Deelnemer.groupnr,Deelnemer.deelnemer_id)"></input>
</div>
<div class="col-md-3" ng-init="getSponsorName(Deelnemer.sponsor)">
{{ SponsorName }}
</div>
</div>
我想用getter getSponsorName(Deelnemer.sponsor)向Sponsorname显示。
对于用户ID 1,Deelnemer.sponsor = 987,对于用户ID 2,Deelnemer.sponsor = 789
$scope.getSponsorName = function(SponsorId) {
if(SponsorId == 987) {
$scope.SponsorName = "Foo";
}
if(SponsorId == 789) {
$scope.SponsorName = "Bar";
}
}
{{SponsorName}}的输出适用于两个用户&#34; Bar&#34;而Deelnemer赞助商对两者都不同?
我想这很简单,我做错了但我不知道/知道它。
提前致谢。
答案 0 :(得分:1)
您获得相同的值,因为您对所有行使用相同的模型,请尝试以下
HTML
<div class="col-md-3" ng-init="getSponsorName(Deelnemer)">
{{ Deelnemer.SponsorName }}
</div>
JS
$scope.getSponsorName = function(Deelnemer) {
if(Deelnemer.SponsorId == 987) {
Deelnemer.SponsorName = "Foo";
}
if(Deelnemer.SponsorId == 789) {
Deelnemer.SponsorName = "Bar";
}
}