如何增加角指令元素的属性 [英] How to add attributes of element to angular directive

查看:135
本文介绍了如何增加角指令元素的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的角。我想写拥有的一切,我在HTML中使用时,添加到它的属性的指令。例如:

I'm new to angular. I want to write a directive which has all the attributes that I added to it when using in html. For example:

这是我的指令

'use strict';
app.directive('province', function($compile) {
    return {
        restrict: 'E',
        link: function (scope, element, attrs, controller) {
            var markup = "<select></select>";
            var elem = angular.element(element);
            elem.replaceWith($compile(markup)(scope));
         }
    };

})

HTML

<province class="form-control" data-target"elemntId"></province>

我希望我的&LT;选择&GT; 包含我添加在HTML中的指令类和其他属性。

I want my <select> contain the class and other attributes that I added to directive in html.

这是我想要的输出:&LT;选择类=表格控数据目标=elementId&GT;&LT; /选择&GT;

我用 angular.element(元素).attr(attr)使用; ,但它不工作;

任何帮助AP preciated提前。

Any help is appreciated in advance.

修改

我希望所有存在的链接功能ATTRS属性添加到标记

I want all the attributes that exist in attrs of link function to be added to markup.

推荐答案

根据您的需求,你并不需要自己编译。您可以使用模板,而是进行更换。

Depending on your needs, you don't need to compile yourself. You can use template and replace instead.

app.directive('province', function() {
    return {
        restrict: 'E',
        template: '<select></select>',
        replace: true,
        link: function (scope, element, attrs) {
        }
    };
});

见plnkr

这篇关于如何增加角指令元素的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆