问题描述
我花了不少时间,通过angularjs文档和几个教程读书,我一直在文档如何难以接近颇为惊喜。
我有一个简单,可回答的问题也可能是有用的人寻求拾取angularjs:
什么是angularjs指令?? 的
应该有一个指令某处简单,precise定义,但提供这些令人惊讶的无用的定义:
-
的 的:指令仅在角一个独特而强大的功能。指令让你创造新的HTML语法,特定于应用程序。
-
的在 的:指令是教HTML新方式招数。 DOM在编译指令对HTML匹配和执行。这使得指令进行注册的行为,或转换DOM。
-
有一个指令当中,讽刺的是,似乎认为观众已经明白它们是什么。
会有人能够提供,清晰的参考,的指令是什么precise定义,解释道:
- 这是什么(见的jQuery 的明确使命angularjs的
谢谢!
A directive is essentially a function that executes when the Angular compiler finds it in the DOM. The function(s) can do almost anything, which is why I think it is rather difficult to define what a directive is. Each directive has a name (like ng-repeat, tabs, make-up-your-own) and each directive determines where it can be used: element, attribute, class, in a comment.
A directive normally only has a (post)link function. A complicated directive could have a compile function, a pre-link function, and a post-link function.
The most powerful thing directives can do is extend HTML. Your extensions are a Domain Specific Language (DSL) for building your application. E.g., if your application runs an online shopping site, you can extend HTML to have "shopping-cart", "coupon", "specials", etc. directives -- whatever words or objects or concepts are more natural to use within the "online shopping" domain, rather than "div"s and "span"s (as @WTK already mentioned).
Directives can also componentize HTML -- group a bunch of HTML into some reusable component. If you find yourself using ng-include to pull in lots of HTML, it is probably time to refactor into directives.
Directives are where you manipulate the DOM and catch DOM events. This is why the directive's compile and link functions both receive the "element" as an argument. You can
- define a bunch of HTML (i.e., a template) to replace the directive
- bind events to this element (or its children)
- add/remove a class
- change the text() value
- watch for changes to attributes defined in the same element (actually it is the attributes' values that are watched -- these are scope properties, hence the directive watches the "model" for changes)
- etc.
In HTML we have things like
<a href="...">
, <img src="...">
, <br>
, <table><tr><th>
. How would you describe what a, href, img, src, br, table, tr, and th are? That's what a directive is. 这篇关于什么是angularjs指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!