How to access the username out of a ng-repeat

2019-08-27 14:13发布

I have a list of users generated with ng-repeat and data from db, which works fine. Now when you click on a user, you get a popup box. I would like to display the name of the selected user in this popup box, but I don't know how to access the username because the ng-repeat does not take place in the popup box.

Note that I work with angular material

Part of my html code:

<!-- START SIDEBAR -->
    <div id="logo-wrap">
      <img id="logo" src="assets/images/logo2.png" alt="Zazzle Logo" >
    </div>
    <div id="sidebar" class="md-whiteframe-z4" ng-data-color="">
      <div style="height: 80px;"></div>
      <div class="userList" id="usrL">
        <li id="customLI" ng-repeat="user in users" id="userPos" class="active circular md-whiteframe-z2" style="background-color: {{ user.color }} " ng-click="showPopUpDeletionConfirmation($event, user._id); " ng-data-id="{{ user._id }}">
          <div ng-if="user._id==activeUser" class="wrapperImageCurrentUser" id="marker_active_user"> </div>
          <p class="initials" id="userValue" style="top: {{ user.top }};" >
            <custom id="user._id"></custom>
            {{user.initials}}
            <!-- {{user.email}} -->
          </p>
          <md-tooltip>{{user.name}}</md-tooltip>
        </li>

      </div>
    </div>
    <!-- END SIDEBAR -->

The code for the popup box (html) resides in the file dialog1.tmpl. This file is just the layout of the popup box nothing relevant to share the code in this question

Here is a visual of my user list and popup box -> https://gyazo.com/694f65c5269cbca910ec6989ee5a77c2

2条回答
地球回转人心会变
2楼-- · 2019-08-27 14:28

Change this:

ng-click="showPopUpDeletionConfirmation($event, user._id);"

to this:

ng-click="showPopUpDeletionConfirmation($event, user);"

and access user object in the popup

EDIT:

You need also to change the showPopUpDeletionConfirmation with this:

$scope.showPopUpDeletionConfirmation = function (ev, user) {
        $mdDialog.show({
            controller: 'DialogDeleteUserController',
            templateUrl: 'confirmDeletion.tmpl.html',
            //parent: angular.element(document.body),
            locals: {
                userId: user._id,
                selectedUser: user.name,
            },
            targetEvent: ev,
            hasBackdrop: false,
        })
            .then(function (result) {
            if (result) {
                $scope.users = _.filter($scope.users, function (user) {
                    return user._id !== userId;
                })
            }
        });
     }

And then you can access the entire user object in the popup template with $scope.selectedUser or something like {{selectedUser.name}}

查看更多
放荡不羁爱自由
3楼-- · 2019-08-27 14:36

You don't access the user in the popup, you pass the user to the popup.

You are already sending the users id, just send the entire user. Use user.name and user._id after you've send it.

查看更多
登录 后发表回答