代码之家  ›  专栏  ›  技术社区  ›  anatol

无法使用$postLink-AngularJS向元素添加属性

  •  0
  • anatol  · 技术社区  · 7 年前

    我有一个简单的组件,我试图做的是向我的输入元素添加一个属性 multiple

    app.component('myComponent', {
        templateUrl: 'tmpl.html',
        bindings: {
            str: '@'
        },
        controller: function () {
            var ctrl = this;
            ctrl.$postLink = function () {
                $('#myInputId').attr('multiple', '');
            }
        }
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   anatol    7 年前

    这就是解决方案:

    app.component('myComponent', {
        templateUrl: 'tmpl.html',
        bindings: {
            str: '@'
        },
        controller: function ($element) {
            var ctrl = this;
            ctrl.$postLink = function () {
                $element.find('input').attr('multiple', 'multiple');
            }
        }
    }
    

    input 当时在DOM中还不存在 $postLink $element 将服务导入控制器并使用它来操作DOM。