UI-Bootstrap将点击值发送到另一个控制器



我有一个主控制器,其中我称为模态。在这种模式中,我有一个名为名称的模型,我想检索该字段以存储在我的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控制器中检索

以基于承诺的模态(例如$ 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);
});

有关更多信息,请参见

  • UI -Bootstrap指令API参考 - $ UIBMODAL

最新更新