How can I transclude into an attribute?

前端 未结 4 1679
独厮守ぢ
独厮守ぢ 2020-12-14 02:06

Is is possible to somehow use ngTransclude for an attribute value, instead of replacing inner HTML content? For example this simple directive

va         


        
4条回答
  •  旧巷少年郎
    2020-12-14 02:44

    I know originally your question was about transclusion, but this problem is MUCH more succinctly solved using an attribute.

    var testapp = angular.module('testapp', [])
    
    testapp.directive('tag', function() {
      return {
        template: '

    {{url}}

    ', restrict: 'E', scope: { url: '@' } } });

    and your html

    
    

    The translation:

    foo

    Also, with the very latest version of Angular, there is a feature called "one-time binding" that is perfect for situations just like this where you only want/need to fulfill the interpolated value one time, upon initialization. The syntax looks like this:

    {{::url}}
    

    Just replace all instances of {{url}} in your template with the above.

提交回复
热议问题