2016-06-20 109 views
1

我试图绑定角度js组件中的通用属性。我的组件包含文本框,我需要的是我想将html属性传递给组件模板。 即将HTML属性传递给角度js 1.5组件

<sample-component generic-attribute="ng-class='hello'"></sample-component> 

这是我的主要html组件。我需要将ng-class传递给我的组件模板。

(function() { 
     angular.module('myApp').component('sampleComponent',{ 
      bindings: { 
       genericAttribute:'@?' 
       }, 
      controllerAs:'ctrl', 
      controller: 'sampleController',   
      template:'<input type="text" {{ctrl.genericAttribute}} > 
      }); 
})(); 

是否可以传递整个属性并将其绑定到模板中。

+0

如果你想,你可以传递类中的类。为什么你还想传递属性是否有原因? – gyc

+1

所以它会很普遍。那就是开发人员可以在同一个密钥上传递ng-class,id等。即

回答

1

你可以尝试的东西是在模板生成函数中使用$compile。这应该让你开始:

(function() { 
    angular.module('myApp').component('sampleComponent',{ 
     bindings: { 
      genericAttribute:'@?' 
      }, 
     controllerAs:'ctrl', 
     controller: 'sampleController', 
     template: function(tElement, tAttrs) { 
      return $compile('<input type="text"' + {{tAttrs.genericAttribute}}); 
     } 
     }); 
})(); 
+0

感谢您的回答。所以我不能用字符串模板来实现这一点。我可以吗 ? –

+0

该函数将一个字符串返回给'template',因此两者是等价的。 –

+0

当我尝试这个我得到'$编译未定义'错误。我怎样才能将编译功能传递给我的模板。 –

相关问题