2017-10-05 47 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