Check out this demo . In this demonstration, I have created a list of "names" and I'm trying to retrieve the text of the selected element.
However, whenever I click on an item from the list, I consistently get the same "innerHTML" for all users.
Is my current approach feasible? Or is there a more efficient way to achieve this using Angular?
Thank you!
Here is a snippet of my code:
Index.html (body)
<body ng-app="app" ng-controller="MyCtrl">
<ion-pane>
<ion-header-bar class="bar-stable">
<h1 class="title">Awesome App</h1>
</ion-header-bar>
<ion-content>
<div class="list">
<a class="item" ng-repeat="name in names" id="userId" ng-click="click()">
{{name.name}}
</a>
</div>
</ion-content>
</ion-pane>
app.js
angular.module('app', ['ionic'])
.controller('MyCtrl', function ($scope) {
$scope.names = [
{name: "Phillip"},
{name: "Jane"},
{name: "Marcos"},
{name: "Thomas"}
]
$scope.click = function () {
var selectedUser = document.getElementById("userId").innerHTML;
alert("You selected " + selectedUser);
}
})
Update
This is how my data is structured.
<ion-content>
<div class="list">
<a class="item"
ng-repeat="friend in friends"
ng-click="selectedUser(friend)">
{{friend._serverData.following}}
</a>
</div>
</ion-content>
From this list, I am attempting to extract the text of an item when clicked (selected).
My JavaScript function:
$scope.selectedUser = function (friend) {
alert(friend.friend);
}