angular.js - angularjs的加载编译机制

查看:80
本文介绍了angular.js - angularjs的加载编译机制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

首先说的当下的技术环境:angular1.6,ui-route,require,疑问:
1、ui-route加载的template和动态注册的controller谁先被编译执行?例如:我想获取template的#id的宽必须要$timeout?否则就出现undefine
2、自定义指令的compile是不是先于template加载?还是并行加载?
有点混乱,跪求解答,相关文章和解惑也跪谢!
附加题:{{}}会自动将类型转化为字符串吧?
谢谢,回答部分也非常感谢

解决方案

官方有详细解释,关于编译过程的介绍:

HTML compilation happens in three phases:

  1. $compile traverses the DOM and matches directives.

    If the compiler finds that an element matches a directive, then the
    directive is added to the list of directives that match the DOM
    element.A single element may match multiple directives.

  2. Once all directives matching a DOM element have been identified, the
    compiler sorts the directives by their priority.

    Each directive's compile functions are executed. Each compile
    function has a chance to modify the DOM. Each compile function
    returns a link function. These functions are composed into a
    "combined" link function, which invokes each directive's returned
    link function.

  3. $compile links the template with the scope by calling the combined
    linking function from the previous step. This in turn will call the
    linking function of the individual directives, registering listeners
    on the elements and setting up $watchs with the scope as each
    directive is configured to do.

The result of this is a live binding between the scope and the DOM. So at this point, a change in a model on the compiled scope will be reflected in the DOM.

具体请题主详读 https://docs.angularjs.org/gu... 这篇关于compiler的文章.

这篇关于angular.js - angularjs的加载编译机制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆