0

I'm new to Angular JS. In my plunkr, I have a typeahead that works when I have the typeahead in the html markup. However, when I dynamically generate the html inside my directive, the typeahead no longer works.

Code Here: http://plnkr.co/edit/KdrxptYAnpTmKa7ZuKkM?p=preview

and to take it one step further, when I pass in a function, it still does not work: http://plnkr.co/edit/jqN913hJxuVSFAZxAQt7?p=preview

1 Answer 1

2

It is not a trivial problem you are trying to solve here, I'm afraid. Basically you are bumping into the scoping issue. The typeahead directive evaluates it expression (city for city in cities($viewValue) here) in the scope of the DOM element on which it is placed. The way you written your wrapper directive makes it so the expression is evaluated in the directive's scope which is isolated and doesn't "see" your controllers scope.

The are number of ways around it but probably the simplest one is to link your $compiled-ed element in the scope that is $parent of your directive scope:

var linkedInput = $compile(inputHtml)(scope.$parent);

Here is a working plunk: http://plnkr.co/edit/fLFwIKNqIRbnesMjZBGj?p=preview

The other alternative is to loose an isolated scope and "manually" take care of the 2-way data binding with the help from the $parse service.

Sign up to request clarification or add additional context in comments.

Comments

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.