加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJS – 指令模板动态

发布时间:2020-12-17 08:31:45 所属栏目:安全 来源:网络整理
导读:如何使用动态模板创建指令? 'use strict';app.directive('ngFormField',function($compile) {return { transclude: true,scope: { label: '@' },template: 'label for="user_email"{{label}}/label',// append replace: true,// attribute restriction rest
如何使用动态模板创建指令?
'use strict';

app.directive('ngFormField',function($compile) {
return {
    transclude: true,scope: {
        label: '@'
    },template: '<label for="user_email">{{label}}</label>',// append
    replace: true,// attribute restriction
    restrict: 'E',// linking method
    link: function($scope,element,attrs) {
        switch (attrs['type']) {
            case "text":
                // append input field to "template"
            case "select":
                // append select dropdown to "template"
        }
    }
  }
});
<ng-form-field label="First Name" type="text"></ng-form-field>

这是我现在的,它正在正确显示标签。但是,我不知道如何附加额外的HTML到模板。或者将2个模板合并为1。

有类似的需求。 $ compile做这项工作。 (不完全确定如果这是“的”方式做到这一点,仍然工作我的方式通过角)

http://jsbin.com/ebuhuv/7/edit – 我的勘探测试。

需要注意的一点(根据我的例子),我的一个要求是,一旦你点击保存,模板将根据类型属性改变,并且模板是非常不同的。所以虽然,你得到数据绑定,如果需要一个新的模板,在那里,你将不得不重新编译。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读