Angularjs Modal - 当模态打开时如何动态设置模态内容

时间:2017-10-05 07:53:07

标签: angularjs dynamic modal-dialog

当模态打开/激活时,如何为模态内容设置新值?

以下是示例代码。

angular.module('plunker', ['ui.bootstrap']);
var ModalDemoCtrl = function ($scope, $modal, $log, $timeout) {
    $scope.test = "test variable"
    var modalInstance = $modal.open({
      templateUrl: 'myModalContent.html',
      controller: ModalInstanceCtrl,
      resolve: {
        test: function () {
          return $scope.test;
        }
      }
    });

$timeout( function(){
            $scope.test = "Hello World!";
        }, 5000 );


};

var ModalInstanceCtrl = function ($scope, $modalInstance, test) {

  $scope.test = test;
  $scope.$watch('test',function (newValue, oldValue) {
    $scope.test = test;
  });
};

在父控制器中,我使用"测试变量"初始化模态1。超时(5秒)后,我希望模态变量更改为" Hello World"没有关闭模态。

可以测试here

2 个答案:

答案 0 :(得分:4)

在ModaInstanceCtrl中使用$ timeout函数,然后相应地更改变量的范围值。以下是示例代码:

angular.module('plunker', ['ui.bootstrap']);

var ModalDemoCtrl = function ($scope, $modal, $log) {
    $scope.test = "test variable"
    var modalInstance = $modal.open({
      templateUrl: 'myModalContent.html',
      controller: ModalInstanceCtrl,
      scope: $scope,
      resolve: {
        test: function () {
          return $scope.test;

        }
      }
    });
};

var ModalInstanceCtrl = function ($scope, $modalInstance, $timeout, test) {

    $timeout(function(){
        $scope.test = "variable" 
    }, 2000);

};

http://plnkr.co/edit/f1zhkWH6aEtZDoKv8egO?p=preview

答案 1 :(得分:3)

您可以直接将控制器范围绑定到类似

的模态
var modalInstance = $modal.open({
      templateUrl: 'myModalContent.html',
      controller: ModalInstanceCtrl,
      scope: $scope
    });

Here is working plunker