2

I am building a rather complex directive in which I need access to ng-model on a specific element in the template which is enclosed into ng-if directive... I have a plunker and the explanation on how to see the issue is below.

In the .compile function of the directive I have a line of code that looks like this

ngModel = elm.find('textarea').controller('ngModel');
console.log(ngModel);

In my template, I have a something like this:

<div  ng-if="views.view">
    <textarea ng-model="exp.val"></textarea>
</div>

When I use ng-show my console.log gets an object of ng-model, no problem ...

The requirement is very strict however and I have to use ng-if. When I use ng-if my console log gets undefined

The actual working version of the problem exists in this plunker

If you change the line 6 in template.html into ng-if you can see the behavior.

How do I have to write this line to retrieve the model when inclosed in ng-if.

ngModel = elm.find('textarea').controller('ngModel');

I also tried using attach-if directive by Simon Sparks. This directive is pretty cool, it preserves the scope unlike ng-if so if you specifically need to not render HTML but you need to preserve scope it works great.

I still have the same problem with invoking ngModel as I am doing it but because of applying custom filters in the directive I have to update ng-model in this way.

I am this one step away from finishing this directive. Hoping someone can help.

7
  • ng-if stops the element from being added to the DOM— you can't retrieve it using find() Commented Feb 10, 2014 at 2:12
  • cwap,, you're right,, the element is not there,,, :/ .. suggestions on how to bypass this and keep the non rendering... When I don't use ngModel = elm.find... and I use attach-if ,, it works,, I need a way to apply the filter conditionally still... Commented Feb 10, 2014 at 2:15
  • What is the intended outcome... to display the textarea with model data? Commented Feb 10, 2014 at 2:32
  • yes,, to display textarea with model data and update the model from textarea,, BUT I have to use the markdown filter conditionally,, update the view with markdown converted html and keep the source in the model. I am using the ng-if because I can't have the element rendered (security related issue - it is a requirement) Commented Feb 10, 2014 at 2:58
  • Hey,, I found a workaround that enables me not to have to search for ngModelCOntroller,, I would still prefer doing it this way if anything crosses your mind though.. so feel free to give input if you have time to spare. Thanks for the help! Commented Feb 10, 2014 at 3:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.