简体   繁体   English

从父控制器AngularjS访问弹出控件

[英]Access Popup controls from parent controller AngularjS

On my main page (Index.html) , on click of a specific icon i am opening a uibmodal (bootstrap) pop-up by calling a respective controller method. 在我的主页面(Index.html)上,单击特定图标,我将通过调用相应的控制器方法打开一个uibmodal(bootstrap)弹出窗口。 The popup is opening perfectly fine. 弹出窗口打开完美。 After that couldn't capture the pop-control's button click events (Agree,Disagree) from the parent controller. 之后无法从父控制器捕获弹出控件的按钮单击事件(同意,不同意)。

Controller.js: Controller.js:

function CameraController($scope,$uibModal) {
        var vm = this;

        vm.captureScreenShot = captureScreenShot;
        vm.openCameraAgreement = openCameraAgreement;


        /* Opens the agreement before capturing the screen shot */
        function openCameraAgreement() {
            //open the agreement
            var instance = $uibModal.open({
                animation: true,
                templateUrl: 'app/components/capture/cameraAgreement.html',
                controller: CameraController,
                controllerAs: 'vm',
                size: 'sm-2'
            });
            vm.cancel = function () {
                instance.close();
            };
            vm.agree = function () {
                instance.close();
                captureScreenShot() //call this method to do something
            };
        }

        function captureScreenShot($event) {
            //do something to capture the screen shot
        }
}

Index.html: index.html的:

<li ng-controller="CameraController as camera">
<a ng-click="camera.openCameraAgreement()">
</a>
</li>

cameraAgreement.html cameraAgreement.html

<a class="btn btn-primary saveCamera" ng-click="vm.agree()">Agree</a>
<a class="btn btn-primary" ng-click="vm.cancel()">Disagree</a>

Basically i wanted to know if the user clicked agree or disagree on the pop-up, so that. 基本上我想知道用户点击是否同意或不同意弹出窗口,以便。 Based on vm.agree() and vm.cancel() are not being called, no errors too. 基于vm.agree()和vm.cancel()没有被调用,也没有错误。 Am i doing any thing wrong with the scope or controller referencing ? 我对示波器或控制器引用有什么问题吗? Kindly help ! 请帮忙!

Try adding the CammeraController $scope to your modal instance, and bind your functions to the $scope, like this: 尝试将CammeraController $作用域添加到模态实例,并将函数绑定到$ scope,如下所示:

var instance = $uibModal.open({
            animation: true,
            templateUrl: 'app/components/capture/cameraAgreement.html',
            controller: CameraController,
            controllerAs: 'vm',
            size: 'sm-2',
 //Add the parent scope
            scope: $scope
        });
        $scope.cancel = function() { ... }

EDIT: Yes the scenario is that you're registering the same controller with two different controllerAs properties so angular might throw the error inside but not every error gets shown on the console. 编辑:是的情况是你正在使用两个不同的controllerAs属性注册相同的控制器,因此angular可能会抛出错误但不会在控制台上显示每个错误。 I think what you can do is you can : 1) either create a new controller (function) for modal, and move the functions inside modal 2) use bindToController to pass the functions from your camera controller to the modal controller. 我认为你可以做的是:1)为模态创建一个新的控制器(函数),并在模态中移动函数2)使用bindToController将函数从摄像机控制器传递给模态控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM