To open a page, you need to first initiate a $http request to get the data from the background, and then pass certain fields of the data as parameters to the directive for compilation.
The problem now is that the code in the directive seems to be executed as soon as the page is opened. Because the data has not been obtained yet, an error will be reported. How to adjust?
Tried and succeeded: first get the code data and then use $compile to dynamically compile the directive and then append it to the dom.
Want to know other good methods.
曾经蜡笔没有小新2017-05-15 17:14:25
1. You can use Promise to encapsulate the AJAX request, and then perform subsequent operations after receiving the result in the instruction.
2. You can directly use $watch in the instruction to check whether the correct data is returned, and then perform subsequent operations. However, it is recommended that $watch is only used to check the data update status and not perform other operations.
为情所困2017-05-15 17:14:25
It’s not clear what your requirements are. You just described a directive value-passing compilation problem
伊谢尔伦2017-05-15 17:14:25
Tell us how to bind the data object to the DOM, use the instruction to get the data on the DOM, and then request the data in the controller