angular.js - Execution order problem, angular needs to compile the directive after getting the data in the background
高洛峰
高洛峰 2017-05-15 17:12:25
0
3
769

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.

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(3)
曾经蜡笔没有小新

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.

为情所困

It’s not clear what your requirements are. You just described a directive value-passing compilation problem

伊谢尔伦

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template