所以我有一个聊天应用程序,在home.html中会显示用户的好友列表,我用ng-repeat来显示好友。这是代码:
<div class="scrollable">
<div class="scrollable-content">
<div class="list-group">
<div class="list-group-item">
<h5>Welcome <b>{{ datauser['data']['nama'] }}</b></h5>
<input type="search" class="form-control app-search" placeholder="Search.." data-ng-model="search" />
<div class="list-group-item media" href="#" ng-repeat="friend in datauser['data']['friends'] | filter : {nama : search}" data-ng-click="chatWith(friend.userid , friend.nama)" data-ng-class="(friend['ischat'] ? 'blokchat' :'')">
<div class="pull-left">
<i class="fa fa-user chat-user-avatar"></i>
</div>
<div class="media-body" >
<h5 class="media-heading">{{friend.nama}} <span class="badge bg-danger" data-ng-if="friend['ischat']">*</span></h5>
</div>
</div>
</div>
</div>
</div>
这个朋友列表中的朋友在他们成为朋友的时候被安排,所以新朋友将在列表的底部。我的问题是如何从朋友列表中获取数组的值?我将在语音识别中使用此值。例如,如果我想与'test'聊天,我会说“Chat whit friend number 2”。
这是语音识别码:
$scope.recog = function() {
var recognition = new SpeechRecognition();
recognition.onresult = function(event) {
var msg = 'Sorry, there is no such a command like that';
var result = event.results[0][0].transcript;
switch(result){
case 'go to home':
$location.path('/home');
break;
case 'go to add friend':
$location.path('/addfriend');
break;
case 'go to friend request':
$location.path('/friendrequest');
break;
case 'go to pending request':
$location.path('/penddingrequest');
break;
case 'add':
$scope.addfriends();
break;
case 'send':
$scope.sendMessage();
break;
default:
navigator.notification.alert(msg, '', 'Undefined Command!','ok');
break;
};
$scope.$apply()
};
recognition.start();
};
那么,我如何从ng-repeat中获取值?谢谢
答案 0 :(得分:1)
我认为最简单的方法是使用与ng-repeat中相同的过滤链,然后添加所需元素的项索引。
在模板中它将是
{{ (friend in datauser['data']['friends'] | filter : {nama : search})[yourElementIndex] }}
如果你需要在控制器内部进行,那么你需要使用$ filter服务。将其注入控制器并在那里进行过滤:
var filteredElements = ($filter('filter')(datauser['data']['friends'], {nama : search}))
var yourElement = filteredElements[yourElementIndex]
<强>加入:强> 例如,您可以创建函数startChatWithFriend(i);
$scope.startChatWithFriend = function(i) {
var filteredFriends = ($filter('filter')(datauser['data']['friends'], {nama : search}));
var friendToChatWith = filteredFriends[i];
//now friendToChatWith contains friend that have number i
....
.... other code using friendToChatWith
}
答案 1 :(得分:1)
所以,从你给我的代码中,我将它与$ index混合,因为我读取$ index的类型是数字。这是混合之后的代码:
$scope.recog = function() {
var recognition = new SpeechRecognition();
recognition.onresult = function(event) {
var msg = 'Sorry, there is no such a command like that';
var filteredElements = ($filter('filter')(datauser['data']['friends'], {nama : search}));
var friendelement = filteredElements[$index];
var result = event.results[0][0].transcript;
switch(result){
case 'login':
$scope.loginFn();
break;
case 'sign up':
$location.path('/register');
break;
case 'register':
$scope.registerFn();
break;
case 'cancel':
$scope.cancelregisterFn();
break;
case 'chat with friend number ' + friendelement:
$scope.chatWith(friend.userid , friend.nama);
break;
case 'go to home':
$location.path('/home');
break;
case 'go to add friend':
$location.path('/addfriend');
break;
case 'go to friend request':
$location.path('/friendrequest');
break;
case 'go to pending request':
$location.path('/penddingrequest');
break;
case 'add':
$scope.addfriends();
break;
case 'send':
$scope.sendMessage();
break;
default:
navigator.notification.alert(msg, '', 'Undefined Command!','ok');
break;
};
$scope.$apply()
};
recognition.start();
};
实际上在我的语音识别中还有其他命令。但是在我添加var filteredElements = ($filter('filter')(datauser['data']['friends'], {nama : search})); var friendelement = filteredElements[$index];
和case 'chat with friend number ' + friendelement: $scope.chatWith(friend.userid , friend.nama); break;
之后,我的语音识别并未捕获任何列出的命令。我不知道哪里错了。
为什么我使用'chat with friend number ' + friendelement
,所以我不需要做一,二,三,直到上帝才知道。根据我的逻辑,如果我使用friendelement,用户可以说每个号码只要总朋友。我错了吗?