ui-bootstrap将点击值发送到另一个控制器

时间:2019-05-27 18:21:24

标签: angularjs angular-ui-bootstrap angular-ui-modal

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

f1
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'));
        };
    }

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

1 个答案:

答案 0 :(得分: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,将诺言作为实例对象的result属性附加:

var modalInstance = $scope.openModal();

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

有关更多信息,请参见