1

Is there a way to get transclude content after it has been interpolated using $transclude in a controller? I want the transclude to be performed without any alteration, but I need the interpolated value.

For example I have a component that transcludes an interpolated string:

<my-transclude-component>{{ vm.someTextToTransclude }}</my-transclude-component>

Using $transclude in my controller I can do this to get the transcluded content, but it gives the uninterpolated value of {{ vm.someTextToTransclude }} as a string instead of Hello World:

vm.$onInit = function() {

    $transclude(function(clone) { 
        console.log(clone.text()); 
    });
}

I know I can bind it to the component, but that's not the point of this question. Currently this is how it is being done since the content isn't interpolated using $transclude:

<my-transclude-component my-text="{{ vm.someTextToTransclude }}"></my-transclude-component>
mtpultz
  • 17,267
  • 22
  • 122
  • 201
  • Is `vm` the scope/controller of the component or do you like to add the text from parent controller and transclude that in the component? Why do you like to transclude instead of passing that variable as one-way binding? – AWolf Feb 07 '17 at 08:55
  • `vm` is the controller scope reference. I'm actually doing it through binding, but I was investigating how to grab the transclude content for another solution, which afterwards kind of morphed into a bit of curiosity that I couldn't figure out if it was possible. – mtpultz Feb 07 '17 at 09:15
  • It's not clear to me what you are trying to do. Do you want to compile the expression `{{ vm.someTextToTransclude }}` and then transclude that in the component? – AWolf Feb 07 '17 at 09:52
  • 1
    Something like in this [fiddle](https://jsfiddle.net/awolf2904/kLamhhLa/) will do that. Or do you need something else? – AWolf Feb 07 '17 at 10:05
  • I was hoping that the alert might contain `Hello World!` instead of `{{ vm.someTextToTransclude }}` like in this [example](https://jsfiddle.net/rbnqmy8y/). I'm guessing I'd have to do something ridiculous like setup a watch, or run $compile on the string to make this work. I'm guessing I should just drop this bit of discovery. Sorry for wasting your time – mtpultz Feb 07 '17 at 10:21
  • 1
    You could add something in `$postLink` to get the compiled expression. Please have a look [here](https://jsfiddle.net/awolf2904/kLamhhLa/2/). – AWolf Feb 07 '17 at 10:49
  • Thanks that does work. I see what you did. You used a $timeout with no delay to get the value after the current digest cycle has completed so interpolation has occurred. – mtpultz Feb 07 '17 at 18:37

0 Answers0