简体   繁体   English

如何在Angular Modal服务中将输入值传递给控制器

[英]How to pass input value to controller in Angular Modal service

Using this angular modal service: 使用角度模态服务:

app.service('modalService', ['$modal',
    function ($modal) {

        var modalDefaults = {
            backdrop: true,
            keyboard: true,
            modalFade: true,
            templateUrl: '/templates/modal.html'
        };

        var modalOptions = {
            closeButtonText: 'Close',
            actionButtonText: 'OK',
            headerText: 'Proceed?',
            bodyText: 'Perform this action?'
        };

        this.showModal = function (customModalDefaults, customModalOptions) {
            if (!customModalDefaults) customModalDefaults = {};
            customModalDefaults.backdrop = 'static';
            return this.show(customModalDefaults, customModalOptions);
        };

        this.show = function (customModalDefaults, customModalOptions) {
            //Create temp objects to work with since we're in a singleton service
            var tempModalDefaults = {};
            var tempModalOptions = {};

            //Map angular-ui modal custom defaults to modal defaults defined in service
            angular.extend(tempModalDefaults, modalDefaults, customModalDefaults);

            //Map modal.html $scope custom properties to defaults defined in service
            angular.extend(tempModalOptions, modalOptions, customModalOptions);

            if (!tempModalDefaults.controller) {
                tempModalDefaults.controller = function ($scope, $modalInstance) {
                    $scope.modalOptions = tempModalOptions;
                    $scope.modalOptions.ok = function (result) {
                        $modalInstance.close(result);
                    };
                    $scope.modalOptions.close = function () {
                        $modalInstance.dismiss('cancel');
                    };
                }
            }

            return $modal.open(tempModalDefaults).result;
        };

    }]);

I'm having trouble understanding how to pass values from the modal (which has an input ) to the controller. 我在理解如何将值从模态(具有input )传递到控制器时遇到麻烦。

This is my modal: 这是我的模态:

<input type="text" class="form-control" id="{{modalOptions.inputName}}" name="{{modalOptions.inputName}}" data-ng-model="modalOptions.inputVal" data-ng-if="modalOptions.inputName"  />
<button type="button" class="btn"
            data-ng-click="modalOptions.close()">{{modalOptions.closeButtonText}}</button>
<button class="btn btn-primary"
            data-ng-click="modalOptions.ok();">{{modalOptions.actionButtonText}}</button>

Controller: 控制器:

$scope.addTopic = function () {

        var modalOptions = {
            closeButtonText: 'Cancel',
            actionButtonText: 'Create Topic',
            inputName: 'topicName'
        };

        modalService.showModal({}, modalOptions).then(function (result) {
            // I tried...
            var input = $scope.inputName; // and...
                input = result;
            $log.log("Adding topic '" + input + "' to publication no " + $scope.publication.id);
        });
    }

So the input is an option in modalOptions but when the user enters a value and clicks ok, nothing is sent to the controller. 因此,输入是modalOptions一个选项,但是当用户输入一个值并单击“确定”时,没有任何内容发送到控制器。 $scope.inputName returns undefined and so does result . $scope.inputName返回undefinedresult也是如此。

Ideally, I want to end up with an object like so { inputs : {name: 'inputName' , value: 'abcde'} } . 理想情况下,我想以这样一个对象结束{ inputs : {name: 'inputName' , value: 'abcde'} }

Try the resolve method in Angular UI Bootstrap 在Angular UI Bootstrap中尝试resolve方法

var modalOptions = {
    resolve: {
        myvar: function () {
            return $scope.myvar;
        }
    }
};

modalService.showModal(modalOptions);

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

相关问题 Angular Modal Service如何在模式弹出窗口中编写函数。 Orelse如何在Modal Pop Up内部传递新的控制器功能 - Angular Modal Service how to write the function inside modal popup. Orelse How to pass a new controller function inside Modal Pop Up 如何将输入标签值传递给(单击)函数并在服务角度 8 中使用 - how to pass input tag value to a (click) function and use in service angular 8 Angular 将输入传递给离子模式 - Angular Pass Input to Ionic Modal 如何以角度将值传递到指令控制器? - How to pass a value into a directives controller in angular? 将当前控制器实例传递给模态服务 - Pass current controller instance to modal service 将 Angular 模态数据传递给主控制器 - Pass Angular modal data to main controller 如何将控制器传递到模式视图 - How to pass controller into modal view 如何在Angular中将输入值作为参数传递给路由器 - How to pass input value as parameter to router in Angular 如何将数据从定制服务正确传递到角度控制器 - how to properly pass the data from a custom service to an angular controller 如何传递嵌套服务并获得角度工厂对控制器的响应 - How to pass the nested service and get the response from angular factory to controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM