首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular-Formly:模式按钮不工作onChange事件

Angular-Formly:模式按钮不工作onChange事件
EN

Stack Overflow用户
提问于 2017-04-04 03:11:56
回答 2查看 520关注 0票数 0

我正在尝试弹出一个自定义复选框的onChange事件的模式。模式显示正常,但不能使按钮工作:(。我哪里做错了?

代码语言:javascript
复制
{
                    key: 'coordinatesToLocateSite',
                    className: 'col-sm-8 col-md-8 col-lg-8',
                    type: 'custom_checkbox',
                    templateOptions: {
                        label: 'Use coordinates to locate site',
                        onChange: function($viewValue, $modelValue, scope) {

                            if ($viewValue === true) {
                                scope.modalInstance = scope.$uibModal.open({
                                    animation: true,
                                    templateUrl: 'address_replace_coordinates_check.html'                                    
                                })
                            }

                            function check() {
                                if (scope.modalInstance) {
                                    scope.modalInstance.close();
                                }
                            }

                            scope.uncheck = function uncheck(keyName) {
                                $scope.model[keyName] = !$scope.model[keyName];
                                if (scope.modalInstance) {
                                    scope.modalInstance.close();
                                }
                            }
                        }                        
                    },

                    expressionProperties: {
                        'templateOptions.disabled': 'formState.disabled'
                    }
                }

在模板中,我的按钮如下:

代码语言:javascript
复制
<div class="modal-footer">
        <button class="btn btn-warning" type="button" ng-click="check()">Ok</button>
        <button class="btn btn-primary" type="button" ng-click="uncheck('coordinatesToLocateSite')">Cancel</button>
    </div>

最后,我也尝试了以下方法。

代码语言:javascript
复制
formlyConfigProvider.setType({
            name: 'custom_checkbox',
            templateUrl: 'bcsa_checkbox.html',
            wrapper: ['bootstrapHasError'],
            apiCheck: function apiCheck(check) {
                return {
                    templateOptions: {
                        onChange: check.oneOfType([check.string, check.func]),
                        label: check.string
                    }
                };
            },
            controller: /* @ngInject */ function($scope, $sce, $uibModal) {
                'ngInject';
                $scope.$uibModal = $uibModal;
                var markerOfRequired = '<span class="red"> \n<strong> \n* \n</strong> \n</span>';
                $scope.labelDisplay = $sce.trustAsHtml($scope.to.label + ($scope.to.required ? markerOfRequired : ''));

                $scope.onChange = onChange;
                $scope.check = check;
                $scope.uncheck = uncheck;

                function onChange($event) {
                    if (angular.isString($scope.to.onChange)) {

                        return $scope.$eval($scope.to.onChange, { $event: $event, $scope: $scope });

                    } else {

                        return $scope.to.onChange($event, $scope);
                    }
                }

                function check() {
                    if (scope.modalInstance) {
                        scope.modalInstance.close();
                    }
                }

                function uncheck(keyName) {
                    $scope.model[keyName] = !$scope.model[keyName];
                    if (scope.modalInstance) {
                        scope.modalInstance.close();
                    }
                }
            }
        });
EN

回答 2

Stack Overflow用户

发布于 2017-04-04 03:57:39

我不能做小提琴来测试,但是读了你的代码,我发现你没有正确地调用函数,ng-click应该访问一个定义在$scope中的对象

因此,尝试将函数声明更改为以下内容

代码语言:javascript
复制
            $scope.onChange = function ($event) {
                if (angular.isString($scope.to.onChange)) {

                    return $scope.$eval($scope.to.onChange, { $event: $event, $scope: $scope });

                } else {

                    return $scope.to.onChange($event, $scope);
                }
            }

            $scope.check = function () {
                if (scope.modalInstance) {
                    scope.modalInstance.close();
                }
            }

            $scope.uncheck = function (keyName) {
                $scope.model[keyName] = !$scope.model[keyName];
                if (scope.modalInstance) {
                    scope.modalInstance.close();
                }
            }
票数 2
EN

Stack Overflow用户

发布于 2017-04-04 04:32:29

我想,当我们打开$uibModal时,我们应该同时传递作用域。所以,我修改了一些代码。

代码语言:javascript
复制
scope.modalInstance = scope.$uibModal.open({
                                animation: true,
                                backdrop: false,
                                keyboard: false,
                                scope: scope,                                                                                                     
                                templateUrl: 'address_replace_coordinates_check.html'
                            })

在控制器中,我访问了它的$parent,而不是访问作用域。

代码语言:javascript
复制
$scope.check = check;
$scope.uncheck = uncheck;

function check() {

                let modalInstance = this.$parent.modalInstance;

                if (modalInstance) {
                    modalInstance.close();
                }
            }

 function uncheck(keyName) {

                let scope = this.$parent;
                let modalInstance = scope.modalInstance;

                scope.model[keyName] = !scope.model[keyName];

                if (modalInstance) {
                    modalInstance.close();
                }
            }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43192518

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档