无法在 angular 指令中添加属性 [英] Not able to add attribute in directive in angular

查看:26
本文介绍了无法在 angular 指令中添加属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的指令中有此代码

   compile: function compile (tElement, tAttributes, transcludeFn) {

        if (tAttributes.drag == 'false') {
            tElement.find('.myclass').removeAttr('draggable');
        }

        //attrs.$set('ngModel', 'new value');
        return {
            pre: function preLink (scope, element, attributes, controller, transcludeFn) {
                // Pre-link code goes here
            },
            post: function postLink (scope, element, attributes, controller, transcludeFn) {

这很好用

但是我想像这样基于布尔值添加属性而不是删除属性

But i want to add attribute instead of remove attribute based on boolean like this

        if (tAttributes.drag == 'true') {
            tElement.find('.myclass').attr('draggable');
        }

但这不起作用.

我想添加后需要重新编译元素,但我不知道该怎么做

I thing i need to recompile element after adding but i don't know how to do it

推荐答案

尝试在指令定义的模板函数中添加属性.

Try adding the attribute in template function of the directive definition.

module.run(function ($templateCache, $http) {
    $http.get('__templateURL__')
      .then(function (response){
        $templateCache.put('__templateID', response.data)
      })
  });

module.directive('x', function ($templateCache) {
    return {
      template: function (tEl, tAttrs) {
        var template = $($templateCache.get('__templateID')); 
        if (tAttrs.drag == 'true') {
          template.find('.myclass').attr('draggable');
        }
        return template[0].outerHTML;
      }
    }
  });

这篇关于无法在 angular 指令中添加属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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