admin管理员组文章数量:1332395
If I had some code similar to this, how would I go about calling the getUserName()
function inside the ng-repeat
. I attempted to do it earlier and it worked, however, it no longer functions.
var user_reviews = [{
user: {
name: "John Doe"
},
review: {
item: "Shure SE215"
}
}]
var app = angular.module("ExApp", []);
app.controller("TestController", function($scope) {
$scope.reviews = user_reviews;
$scope.getUserName = function() {
return $scope.user.name;
}
});
HTML
<div ng-controller="TestController">
<div ng-repeat="review in reviews">
<p>{{review.getUserName()}}</p>
</div>
</div>
If I had some code similar to this, how would I go about calling the getUserName()
function inside the ng-repeat
. I attempted to do it earlier and it worked, however, it no longer functions.
var user_reviews = [{
user: {
name: "John Doe"
},
review: {
item: "Shure SE215"
}
}]
var app = angular.module("ExApp", []);
app.controller("TestController", function($scope) {
$scope.reviews = user_reviews;
$scope.getUserName = function() {
return $scope.user.name;
}
});
HTML
<div ng-controller="TestController">
<div ng-repeat="review in reviews">
<p>{{review.getUserName()}}</p>
</div>
</div>
Share
Improve this question
edited Jun 27, 2016 at 6:40
Ismail RBOUH
10.5k2 gold badges25 silver badges37 bronze badges
asked Jun 27, 2016 at 5:18
helloMundohelloMundo
871 gold badge4 silver badges11 bronze badges
8
- 2 y dont u directly take the name instead calling function <p>{{review.user.name}}</p> – Gayathri Mohan Commented Jun 27, 2016 at 5:22
- Im trying to call a function because I need to do some string manipulation inside the function. And @IsmailRBOUH that doesn't work, I have tried that already. – helloMundo Commented Jun 27, 2016 at 5:25
- If you don't need this as function, you shall not use function. – Patryk Perduta Commented Jun 27, 2016 at 5:25
- What version of Angular do you use? – Patryk Perduta Commented Jun 27, 2016 at 5:26
- 1 your review-variable doesn't have a function called getUserName. As i remember ng-repeat creates a child scope – iiro Commented Jun 27, 2016 at 5:35
4 Answers
Reset to default 4You can pass the index from ng-repeat and return the user.name relevant to that index
<div ng-controller="TestController">
<div ng-repeat="review in reviews">
<p>{{getUserName($index)}}</p>
</div>
</div>
var app = angular.module("ExApp", []);
app.controller("TestController", function($scope){
$scope.reviews = user_reviews;
$scope.getUserName = function(index){
return $scope.reviews[index].user.name;
}
});
but if you are using function for this senario only i prefer do it without the function like this
<div ng-controller="TestController">
<div ng-repeat="review in reviews">
<p>{{review.user.name}}</p>
</div>
</div>
Assuming scope.getUserName() isn't as trivial as it is in your example, you can do this instead.
// Pass the `review` object as argument
$scope.getUserName = function(review){
return review.user.name; // get the user name from it
}
HTML, pass review
as parameter
<div ng-controller="TestController">
<div ng-repeat="review in reviews">
<p>{{getUserName(review)}}</p>
</div>
</div>
//You can directly access the username
<div ng-controller="TestController">
<div ng-repeat="review in reviews">
<p>{{review.user.name}}</p>
</div>
</div>
//or You can pass the review as parameter and return username from that function.
//HTML
<div ng-controller="TestController">
<div ng-repeat="review in reviews">
<p>{{getUserName(review)}}</p>
</div>
</div>
//JS
$scope.getUserName = function(review){
return review.user.name;
}
You should change your code to this.
<div ng-controller="TestController"><div ng-repeat="review in reviews">
<p ng-repeat="userName in review.getUserName(review.name)">{{userName.name}}</p></div></div>
var user_reviews = [
{
user:{
name: "John Doe"
},
review:{
item: "Shure SE215"
}
}
]
var app = angular.module("ExApp", []);
app.controller("TestController", function($scope){
$scope.reviews = user_reviews;
$scope.getUserName = function(userName){
return $scope.user.name;
}
});
本文标签: javascriptCalling a function in ngrepeatStack Overflow
版权声明:本文标题:javascript - Calling a function in ng-repeat? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742283288a2446491.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论