Wrapping angular typeAhead in separate directive

萝らか妹 提交于 2019-12-11 13:29:28

问题


My goal was to wrap angular-ui typeAhead directive into easily re-usable component, that could be used like this:

<input my-person-lookup="myModel.personId" ></input>

I've achieve this by writing my custom directive, and specifying static template for typeahead. Here is Plunker

Now, I would like to build this template dynamically, and then compile it:

  var html = '<input type="text" ng-model="directiveModel.selectedPerson" typeahead=" p as p.name for p in people" typeahead-min-length="1" typeahead-wait-ms="200" typeahead-editable="false" placeholder="type p"></input>';
  element.replaceWith($compile(html)(scope));

Unfortunately this approach didn't work Plunker.

Could anyone tell me what I'm doing wrong?


回答1:


Just move your data to the wrapping controller and remove your isolated scope.

Plunker

Plunker2



来源:https://stackoverflow.com/questions/20363004/wrapping-angular-typeahead-in-separate-directive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!