0

Let's say you have a page where you have a directive for customer search (1st directive). You do a search and you get back a list of customers from a web server.

Now, inside the directive above, you have a 2nd directive, let's call it customerRes, that displays some info for the customers (this is like a small widget reused in many places in your site). Inside the isolated scope of this 2nd directive I have an attribute called results that I bound from the results returned e.g.

The 1st directive has a declaration like

<customer-res results="webserverResult">

and inside the customerRes directive I have something like a

<div ng-repeat="x in results>
{{name}} {{bla}}  ... bla bla
</div>

Now everytime the user clicks the parent directive, it will make a request to the web server and perhaps get different results back.

The question: Does angular actually monitor itself that the value bound to the isolated scope changed outside the directive, or do I need to do something to make sure that it invalidates?

Also, even if you load your page before the results get back, once they get back shouldn't they update the page elements?

4

1 回答 1

0

一个很酷的方法是你自己会发现。不要误会我的意思,但就我个人而言,我在迈出有角的第一步时就这样做了。有助于更深入地理解框架,而不是阅读文档并更容易忘记。

您可以随时添加

scope.$watch('myVariable', function(value) { 
  console.log('myVariable changed to:', value);
})

到你的指令,看看它的价值何时改变。

双向绑定就像一个魅力,即使在整页重新加载。没有你的指令的 JS 代码就不能更精确了。但是当使用隔离作用域时,指令可能需要

scope: {
   myVariable: '='
}
<my-directive my-variable="results.entries"></my-directive>

但请记住,重新加载页面时,角度从零开始。对于每个指令来说,它的链接函数都被调用一次,并且在后台调用了许多其他的东西。关键字是 angular's digest cycle

希望我可以为这个(更广泛的)答案提供帮助。

于 2015-01-14T19:59:38.127 回答