2017-10-05 81 views
0

如何在模态打开/激活时为模态内容设置新值?Angularjs Modal - 如何在模态打开时动态设置模态内容

以下是示例代码。

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

在父控制器,我初始化以“测试变量”和超时后(5秒),我想模态变量的变化为“Hello World”无收模态的模态1日。

可以测试here

回答

2

您可以直接绑定控制范围,模态像里面ModaInstanceCtrl

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

Here is working plunker

+0

谢谢你的男人.. – Sh4m

+0

高兴它帮助.... – jitender

2

使用$超时功能,然后相应地更改变量的范围值。这里是示例代码。

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, test,$timeout) { 

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

}; 

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