Reputation: 633
I have set of json
objects.
User Details have the languages field - which have more than 1 values. Here is my sample json
$scope.languages = [
{id: 1, text: 'English'},
{id: 2, text: 'French'},
{id: 3, text: 'Hindi'},
{id: 4, text: 'Telugu'}
];
$scope.users = [{name: 'first user', status: 1,language:"1"},
{name: 'second user', status: 2,language:"1,2"},
{name: 'third user', status: 3,language:"1,3,4"}];
In my view i want to list the user name and languages.
<li ng-repeat="user in users">{{user.name}} - {{testString}}</li>
I know to do for single value. But for multiple values. I have the logic but i don't know how to implement. I am thinking that. First i have to split the user language string and change into array and then find the index of the language id and then return the language text.
I have the code to return language name from ID.
var foundItem = $filter('filter')($scope.languages, { id: 3 }, true)[0];
var index = $scope.languages.indexOf(foundItem );
$scope.result = $scope.languages[index].text;
So now the problem is how to print the languages next to the user name?
I tried like this
$scope.testString = function() {
return "return test string";
}
{{testString}}
But its not working. If this works we can pass the langugae codes as parameter and i can add the search code inside the testString function.
Thanks
Upvotes: 0
Views: 87
Reputation: 69905
testString
is a function so you cannot use it like {{testString}}
, you have to call that function {{testString()}}
You can simplify your code like this.
$scope.getLanguages = function (langs) {
var l = [];
angular.forEach(langs.split(','), function (lang) {
l.push(findLanguageTextById(parseInt(lang)));
})
return l.join(', ');
}
function findLanguageTextById (langId) {
for(var i = 0;i<$scope.languages.length;i++) {
if ($scope.languages[i].id == langId) {
return $scope.languages[i].text;
}
}
}
I have created a working demo for your problem take a look.
http://plnkr.co/edit/Cdl8y58IExoVSZV6lp76?p=preview
Upvotes: 1
Reputation: 856
I think you are not calling the function,
$scope.testString = (function() {
return "return test string";
})();
Upvotes: 0