foreach循环中带有$ scope函数的变量

时间:2019-04-07 03:28:18

标签: angularjs

$scope.regroup = function () {
    //For each group: Move a group up so that it's put after the same named group.

    angular.forEach($scope.sessions, function (session, i) {

        var moved = $scope.moveMergedGroupsUpOnLeft(session.Groups);
    });

1 个答案:

答案 0 :(得分:-1)

我没有发现任何问题。唯一的事情就是您定义了moveMergedGroupsUpOnLeft函数的地方。

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
  <ul>
      <li ng-repeat="a in arr" ng-click="click(a)">Click {{::a}}</li>
  </ul>
</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope) {
    $scope.arr = [1, 2, 3, 4, 5];
    $scope.click = function (_id) {
        angular.forEach($scope.arr, (id) => {
            if (id === _id) {
                $scope.moveMergedGroupsUpOnLeft(id);
            }
        });
    }

    $scope.moveMergedGroupsUpOnLeft = function (id) {
        alert('clicked ' + id);
    };
});
</script>

</body>
</html>