0

我有一个主控制器,在其中我称之为模态。在这个模式中,我有一个名为 name 的模型,我想检索这个字段以存储在我的 localStorage 中。我在几个网站上搜索,但没有奏效。我的控制器是这样的:

app.controller('gameCtrl', ['$scope', '$uibModal', function($scope, $uibModal) {

    $scope.openModal ()

    $scope.congratulations = function() {
        if ($scope.matchedCard.length == 2) {
            alert('ACABOU')
            clearInterval($scope.interval);
            $scope.finalTime = $scope.timer.innerHTML;
            $scope.player = [{
                // Here i want save $scope._player (modal)  in local storage
                name: $scope._player = player;
                moves: $scope.moves,
                time: $scope.minute + " minutos " + $scope.second + " segundos"
            }]

            if (localStorage.getItem('players')) {
                var totalPlayers = JSON.parse(localStorage.getItem('players'));
                totalPlayers.push({
                    name: $scope.name,
                    moves: $scope.moves,
                    time: $scope.minute + " minutos " + $scope.second + " segundos"
                })
                localStorage.setItem('players', JSON.stringify(totalPlayers));
            } else {
                localStorage.setItem('players', JSON.stringify($scope.player));
            }
            var totalPlayers = JSON.parse(localStorage.getItem('players'));
        };
    }
    $scope.openModal = function() {
        $uibModal.open({
            templateUrl: '../../../pages/component/modal.html',
            controller: function($scope, $uibModalInstance) {
                $scope.savePlayer = function(player) {
                    $scope._player = player;
                    $uibModalInstance.close();
                };
                $scope.cancel = function() {
                    $uibModalInstance.dismiss('cancel');
                }
            }
        })
    }; 

我想发送输入值,所以我可以在 tro 控制器中检索它

4

1 回答 1

0

使用基于承诺的模态,例如 $uibModal,将数据作为参数发送回.close方法:

$scope.openModal = function() {
    return $uibModal.open({
        templateUrl: '../../../pages/component/modal.html',
        controller: function($scope, $uibModalInstance) {
            $scope.savePlayer = function(player) {
                $scope._player = player;
                ̶$̶u̶i̶b̶M̶o̶d̶a̶l̶I̶n̶s̶t̶a̶n̶c̶e̶.̶c̶l̶o̶s̶e̶(̶)̶;̶
                $uibModalInstance.close(player);
            };
            $scope.cancel = function() {
                $uibModalInstance.dismiss('cancel');
            }
        }
    })
};

使用$uibModal,promise 被附加为result实例对象的属性:

var modalInstance = $scope.openModal();

modalInstance.result
  .then(function (player) {
     console.log("Modal closed with:", player);
}).catch(function (reason) {
     console.log("Modal cancelled:", reason);
});

有关详细信息,请参阅

于 2019-05-27T23:29:51.840 回答