首页 > 解决方案 > 如何从模式窗口更改 $scope 变量

问题描述

我的控制器中有这个函数,我将一系列数据作为参数正确接收到该函数。

$scope.changeValues = function(measure,name,valCol) {

var modalInstance = $modal.open({

    templateUrl: 'myModalContent.html',

    backdrop: 'static',

    scope: $scope, 

    controller: function($scope, $modalInstance) {

             $scope.ok = function(){

                 $scope.myVar = "hello";
                 console.log($scope.myVar);
                 modalInstance.close();

             }

    }

});};

我想在我的 $modal.window 中更改 $scope.myVar 变量的值,但是这些更改没有保存。错误在哪里?

<div class="reducedfont" ng-init="loadCtrl()">

<script type="text/ng-template" id="myModalContent.html">
<!-- template for modal -->
    <div class="modal-header">
        <h3 class="modal-title">{{valueName}}</h3>
    </div>
    <div class="modal-body">

    </div>
    <div class="modal-footer">
        <button class="btn btn-primary" ng-click="ok()">Ok</button>
        <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
    </div></script>
<p>{{myVar}}</p>

标签: angularjsmodal-dialogbootstrap-modal

解决方案


如果你想改变

 $scope.changeValues = function(measure,name,valCol) {

    var modalInstance = $modal.open({

        templateUrl: 'myModalContent.html',

        backdrop: 'static',

        scope: $scope, 

        controller: function($scope, $modalInstance) {

                 $scope.ok = function(){

                     $scope.myVar = "hello";
                     console.log($scope.myVar);
                     $modalInstance.close($scope.myVar);

                 }

        }

    })//end of $model


    modalInstance.result.then(function (SelectedItem) {
        setTimeout(function () {
            //SelectedItem is the value return from modal controller on close and $scope.myVar is the variable in the controller where $scope.changeValues function is defined.

            $scope.myVar= SelectedItem;
        }, 500);
    });

    };//end of function

推荐阅读