https://stackoverflow.com/questions/38812824
I read document from AngualrJS : https://docs.angularjs.org/guide/component
And when I am reading to "Example of a component tree" section, I got confused about how a component tree's loading works, because there is nowhere to find the loading order.
How angular find the root directive and nested directive (may be in a template)? and Which component should start work first? I mean, If a nested component() is called earlier than its directive's appearance, will it be called later when its directive appears? How angular knows the appropriate component to call when a directive/template load? Or just iterate all components method? Or load all components first, then according index.html to form a component hierarchy, then call it properly?
Is there anybody kindly explain? thanks a lot!
Thanks estus!, your anwser is really helpful! According to
AngularJS : How does the HTML compiler arrange the order for compiling?
https://stackoverflow.com/a/15044832/2893073
and Pete Bacon Darwin's example :
And this comes from :
How directives are compiled
https://docs.angularjs.org/guide/compiler#how-directives-are-compiled
exmaple:
// app.js
angular.module('compilation', [])
.directive('logCompile', function($rootScope) {
$rootScope.log = "";
return {
controller: function($scope, $attrs) {
$rootScope.log = $rootScope.log + ($attrs.logCompile + ' (controller)\n');
},
compile: function compile(element, attributes) {
$rootScope.log = $rootScope.log + (attributes.logCompile + ' (compile)\n');
return {
pre: function preLink(scope, element, attributes) {
$rootScope.log = $rootScope.log + (attributes.logCompile + ' (pre-link)\n');
},
post: function postLink(scope, element, attributes) {
element.prepend(attributes.logCompile);
$rootScope.log = $rootScope.log + (attributes.logCompile + ' (post-link)\n');
}
};
}
};
})
.directive('terminate', function() {
return {
terminal: true
};
});
index.html
<!doctype html>
<html ng-app="compilation">
<head>
<meta charset="utf-8">
<title>Compilation Demo</title>
<link rel="stylesheet" href="style.css">
<script src="http://code.angularjs.org/1.1.1/angular.js"></script>
<script src="app.js"></script>
</head>
<body>
<div log-compile="parent">
<div log-compile="..child 1">
<div log-compile="....child 1 a"></div>
<div log-compile="....child 1 b"></div>
</div>
<div log-compile="..child 2">
<div log-compile="....child 2 a"></div>
<div log-compile="....child 2 b"></div>
</div>
</div>
<!-- LOG -->
<pre>{{log}}</pre>
</body>
</html>
style.css
div {
padding: 5px;
margin: 5px;
background-color: #EEE;
border: 1px solid #BBB;
}
div > div {
background-color: #DDD;
}
div > div > div {
background-color: #CCC;
}
ol {
list-style: decimal;
margin-left: 30px;
}
-

- 大小: 213.6 KB

- 大小: 117.8 KB
分享到:
相关推荐
数据绑定是AngularJS最强大的特性之一。AngularJS提供了双向数据绑定功能,这意味着开发者不需要手动同步视图和模型之间的数据变化。当模型数据发生变化时,视图会自动更新;反之亦然。 - **双向数据绑定**:通过`...
3. **优化性能**:学习如何通过优化指令的执行顺序或减少不必要的 DOM 操作来提高应用程序的性能。 4. **测试指令**:掌握如何对指令进行单元测试和集成测试,确保它们能够按照预期工作。 #### 六、实际应用场景 1...
- **18.2 指令的执行过程**:AngularJS指令的执行顺序和生命周期是明确的,有助于开发者理解和调试。 - **18.3 基本的自定义方法**:定义自定义指令的方法包括`link`函数、`controller`等。 - **18.4 属性值类型的...
- **内置指令**:如`ng-if`(条件渲染)、`ng-repeat`(循环迭代)、`ng-class`(基于表达式添加类名)等,这些都是AngularJS提供的一系列内置指令,它们极大地提高了开发效率。 - **自定义指令**:允许开发者创建...
在这个工作坊中,我们将深入理解AngularJS的指令系统,它是AngularJS的核心特性之一。 AngularJS的指令是扩展HTML能力的一种方式,允许开发者创建自定义的DOM元素和属性,以实现特定的功能。这些自定义的HTML指令...
总之,当在AngularJS项目中使用Swiper时,遇到循环轮播失效的问题,可以通过调整初始化顺序,利用`setTimeout`延迟初始化,或者结合`observer`和`observeParents`属性来解决。这种方法能确保Swiper在所有数据加载并...
5. Directive的link函数参数顺序:在创建自定义指令时,link函数的参数顺序是固定的,第一个参数是作用域(scope),接着是元素(element),属性(attrs)和控制器(ctrl)。错误的顺序可能导致指令无法正确执行。 ...
在本文中,我们将深入探讨如何将AngularJS中的Angular-UI图片轮播组件封装为一个可重用的指令。Angular-UI库提供了丰富的组件,其中包括图片轮播模块,它可以帮助我们快速实现图片展示的动态切换效果。为了提高代码...
26. AngularJS定义hello指令:`app.directive('hello', function() { return { template: 'Hello, {{name}}!', scope: { name: '@' } }; });` 27. 去除数组重复元素:使用`Set`对象或`filter()`结合`indexOf()`。 ...