简体   繁体   English

用ng-click进行ng-repeat不能正常工作

[英]ng-repeat with ng-click not working properly

i got some new issues in my spa with angularjs. 我在使用angularjs的水疗中心遇到了一些新问题。

THIS ist my HTML: 这是我的HTML:

 <a ng-repeat="friend in chat.friendlist" ng-click="loadChat('{{friend.friend_username}}')" data-toggle="modal" data-target="#chat" data-dismiss="modal" class="list-group-item px-md"> <i class="fa fa-circle text-success text-xs mr-xs"></i> <span>{{friend.friend_username}} - {{friend.gamename}}</span> </a> 

This is my controller: 这是我的控制器:

"use strict";

    app.controller("chatCtrl", ["userService", "socket" , "chatService", "$scope", function (userService, socket , chatService, $scope) {
        var self = this;

        self.friendlist = chatService.friendlist;

        chatService.init(userService.username);

        $scope.loadChat = function (username) {
            console.log(username); //console logs {{friend.friend_username}}
            chatService.chatWith(username);
        }

    }]);

Everything is working properly except the username within the loadChat function. 除了loadChat函数中的用户名,其他所有东西都正常工作。 The username console logs {{friend.friend_username}} and not the value of friend.friend_username as i want to do. 用户名控制台记录的是{{friend.friend_username}},而不是我想要的friend.friend_username的值。 Within the DOM everything looks fine. 在DOM中,一切看起来都很好。 ng-click="loadChat('USERNAME')" is shown properly but in chat controller the username is set as {{friend.friend_username}}. ng-click =“ loadChat('USERNAME')”正确显示,但在聊天控制器中,用户名设置为{{friend.friend_username}}。 I´ve no clue why this happens. 我不知道为什么会这样。 I hope someone can help me to figure this out. 我希望有人可以帮助我解决这个问题。

将用户名直接传递给ngClick ,不要使用字符串插值

<a ng-click="loadChat(friend.friend_username)" >

不需要插值

ng-click="loadChat(friend.friend_username)"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM