ng-repeat and ng-controller on the same DOM element

前端 未结 2 440
萌比男神i
萌比男神i 2021-02-06 03:54

Can we attach ng-controller and ng-repeat to the same DOM element? Fiddle

Here is the HTML:

2条回答
  •  被撕碎了的回忆
    2021-02-06 04:44

    You should break your controller into UserListController and UserController. The list of users should be part of UserListController and the each item can be managed by UserController

    Something like

So the user controller becomes

angular.module("myApp", [])     
    .controller("UserController", ["$scope", function($scope) {
        $scope.selected = false;

        $scope.toggleSelectedUser = function() {
            $scope.user.selected = !$scope.selected;
        };

        $scope.isSelectedUser = function() {
            return $scope.user.selected;
        };
    }]);

提交回复
热议问题