呈现后需要在模式中的元素上添加侦听器

时间:2018-10-11 21:21:23

标签: javascript angularjs bootstrap-modal

我有一个响应于单击按钮而弹出的模式窗口。模态已分配了一个控制器,并且在该控制器内,我具有需要分配侦听器的元素的ID列表,但是,在控制器初始化模态时尚未呈现,因此需要侦听器的元素不会存在。我试图弄清楚模态显示后如何分配侦听器。

创建模态的调用如下:

ModalDialogFactory.showDialog(dialogOptions);

然后在工厂内:

        showDialog: function (modalOptions) {
            var modalConfig = {};
            angular.extend(modalConfig, modalDialogDefaults, modalOptions);
            modalInstance = $modal.open(modalConfig);
            modalInstance.opened.then(function() {
                // In here, I think
            })
            return modalInstance.result;
        }

在模态显示之后,是否有办法采用那个modalInstance并访问其控制器功能?

1 个答案:

答案 0 :(得分:0)

您可以使用ng-init:

在模式HTML中:

<span ng-init="vm.initialize()"></span>
<input ng-model="vm.model" type="text">

在模式控制器中:

vm.model = "new value";

function initialize() {
     $scope.$watch('vm.model', function(newVal, oldVal){
          console.log("value was changed to:"+newVal);
          vm.model.watch = newVal;
     });
}