【问题标题】:Access Popup controls from parent controller AngularjS从父控制器AngularjS访问弹出控件
【发布时间】:2016-03-15 21:28:03
【问题描述】:

在我的主页 (Index.html) 上,单击特定图标时,我通过调用相应的控制器方法打开一个 uibmodal(引导程序)弹出窗口。弹出窗口打开得很好。之后无法从父控制器捕获弹出控件的按钮单击事件(同意,不同意)。

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

索引.html:

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

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>

基本上我想知道用户是否在弹出窗口上单击了同意或不同意,这样。基于 vm.agree() 和 vm.cancel() 没有被调用,也没有错误。我对范围或控制器引用做错了吗?请帮忙!

【问题讨论】:

    标签: javascript jquery html angularjs twitter-bootstrap


    【解决方案1】:

    尝试将 CammeraController $scope 添加到您的模态实例,并将您的函数绑定到 $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() { ... }
    

    【讨论】:

      【解决方案2】:

      编辑: 是的,场景是您正在使用两个不同的 controllerAs 属性注册同一个控制器,因此 angular 可能会在内部抛出错误,但并非每个错误都会显示在控制台上。 我认为你可以做的是你可以: 1)要么为模态创建一个新的控制器(函数),然后在模态中移动函数 2) 使用 bindToController 将函数从相机控制器传递到模态控制器。

      【讨论】:

        猜你喜欢
        • 2014-02-22
        • 1970-01-01
        • 2023-03-10
        • 2016-01-21
        • 1970-01-01
        • 1970-01-01
        • 2018-01-16
        • 2013-07-22
        • 1970-01-01
        相关资源
        最近更新 更多