AngularJS:如何从外部评估指令?
Executable example:
http://jsbin.com/secuz/5/edit?html,js,output
http://jsbin.com/secuz/5/edit?html,js,output
Details
The above failure contradicts my assumption about in what order Angular directives are processed. I would expect that outer directives get run first, then execution propagates inwards.
上述失败与我对Angular指令处理顺序的假设相矛盾。我希望外部指令先运行,然后执行向内传播。
Am I correct to think that this out-of order evaluation is due to ng-if
having higher priority than ng-show
? And if so, shouldn't priorities only be taken into account if two directives are on the same element?
我是否认为这种无序评估是由于ng-如果比ng-show具有更高的优先级?如果是这样,如果两个指令在同一个元素上,那么不应该只考虑优先级吗?
Main question
How can I make sure execution always goes from outside in? Do I need to give the same priority to all the directives I use?
我怎样才能确保执行始终从外部进行?我是否需要对我使用的所有指令给予相同的优先权?
Edit:
Tried 2 additional scenarios:
尝试了另外2个场景:
- If both are
ng-if
s, then the propagation happens from outside-in, just as expected: - 如果两者都是ng-ifs,则传播从外向内发生,正如预期的那样:
[[ '1 if', true ], [ '2 if', true ], [ '1 if', true ], [ '2 if', true ]]
[['1 if',true],['2 if',true],['1 if',true],['2 if',true]]
- However, two
ng-show
s exhibit a counter-intuitive behaviour: - 然而,两个ng-shows表现出反直觉的行为:
[[ '2 show', true ], [ '1 show', true ], [ '2 show', true ], [ '1 show', true ]]
[['2 show',true],['1 show',true],['2 show',true],['1 show',true]]
1 个解决方案
#1
2
Compilation involves these three call-back-bindable steps:
编译涉及这三个可回调绑定的步骤:
compile
- 编
pre-link
- 前链路
post-link
- 后链接
More here.
更多这里。
I am not sure where ng-show and ng-if do their behavior, but I bet you could find out.
我不确定ng-show和ng-if在哪里做他们的行为,但我打赌你可以找到。
If ng-show and ng-if set up the behavior you're testing in the post-link, that actually executes in reverse order, climbing up from the lowest element and back out, so this would explain your finding.
如果ng-show和ng-if设置你在后链接中测试的行为,它实际上以相反的顺序执行,从最低元素爬升并退出,这样就可以解释你的发现。
更多相关文章
- 将对象值传递给指令而不是任何其他数据变量
- Angularjs指令ng-hide在单元格模板中不对网格数据的更改进行绑定
- 当开始使用数据-*时,无法识别AngularJS指令
- $ postLink的角度组件/指令运行得太早
- element.replaceWith在自定义指令的链接中仅在第一次调用时工作
- Mac下MySQL-python安装及EnvironmentError: mysql_config not fo
- Python 2.6.2中的ElementTree处理指令支持?
- 通过指令“ps -l”查看进程信息
- 常见压缩格式的区别,及 Linux 下的压缩相关指令