如何使用ng-click超链接从表单输入文本字段中删除/添加属性

时间:2022-08-25 23:36:39

I am trying to create a directive that will remove the attribute disabled="disabled" from all input field contained in a form by clicking an edit link.

我正在尝试创建一个指令,通过单击编辑链接从表单中包含的所有输入字段中删除属性disabled =“disabled”。

The problem I am facing is that when I click on the link, even though the directive code is being called, I am not passing the form fields, the only thing that the code has access to is the link element.

我面临的问题是,当我点击链接时,即使调用了指令代码,我也没有传递表单字段,代码唯一可以访问的是link元素。

If anyone could help me to make this work, that would be sweet.

如果有人能帮我做这项工作,那就太好了。

Here is the work I have done so far.

这是我到目前为止所做的工作。

My html with directive as attribute:

我的html with directive作为属性:

<a title="Edit record" ab-toggle-fields ng-click="pc.toggle()">
    <i class="fa fa-fw fa-pencil"></i>
</a>

The toggle function in controller, I am not sure if I need this part, took it from an example:

控制器中的切换功能,我不确定我是否需要这个部分,从一个例子中取出它:

vm.toggle = function () {
};

The directive code:

指令代码:

    (function (define, angular) {
    'use strict';

    define(function () {
        var toggleFieldsDirective = function () {
            return {
                scope: {
                    'ngClick': '&'
                },                
                link: function (scope, elem, attrs) {

                    elem.bind('click', function () {

                        if (scope.ngClick){
                            var fields = elem.find(':input').not('button');
                            angular.forEach(fields, function (field) {
                                angular.element(field).removeAttr('disabled');
                            });
                        }

                    });
                }
            }
        };
            return [
                toggleFieldsDirective
            ];
        });
})(define, angular);

1 个解决方案

#1


0  

Really the cleanest way is to simply use the ng-disabled directive on your inputs. Set the value to some variable that gets toggled on the click of your link. Like this:

真正最干净的方法是在输入上简单地使用ng-disabled指令。将值设置为某个变量,该变量在链接点击时切换。像这样:

<body>
  <a href="#" title="Edit record" ng-click="isDisabled = !isDisabled" ng-init="isDisabled=false">
    <i class="fa fa-fw fa-pencil"></i>
  </a>
  <div>
    <input name="inputA" type="text" ng-disabled="isDisabled" />
  </div>
  <div>
    <input name="inputB" type="text" ng-disabled="isDisabled" />
  </div>

#1


0  

Really the cleanest way is to simply use the ng-disabled directive on your inputs. Set the value to some variable that gets toggled on the click of your link. Like this:

真正最干净的方法是在输入上简单地使用ng-disabled指令。将值设置为某个变量,该变量在链接点击时切换。像这样:

<body>
  <a href="#" title="Edit record" ng-click="isDisabled = !isDisabled" ng-init="isDisabled=false">
    <i class="fa fa-fw fa-pencil"></i>
  </a>
  <div>
    <input name="inputA" type="text" ng-disabled="isDisabled" />
  </div>
  <div>
    <input name="inputB" type="text" ng-disabled="isDisabled" />
  </div>