[英]Angular directive not recognizing scope of controller
First of all I know I am going to have to change the title, hopefully I can better form my question based on the answers. 首先,我知道我将不得不更改标题,希望我可以根据答案更好地提出我的问题。
I am working on a breadcrumb using angular (let me know if the link fails) but for some reason it isn't working as I would expect. 我正在使用angular制作面包屑 (让我知道链接是否失败),但是由于某种原因,它无法正常工作。 Everything seems fine but when I click the link to go to the next page (sorry in advance for the annoying alert).
一切似乎都很好,但是当我单击链接转到下一页时(对于烦人的警报,请提前表示抱歉)。 I get the following error...
我收到以下错误...
Uncaught TypeError: Cannot read property 'addCrumbs' of undefined
未捕获的TypeError:无法读取未定义的属性'addCrumbs'
The directive is clearly working so I am guessing it is this line that is causing the issue... 该指令显然是有效的,所以我猜想这是导致此问题的这一行...
angular.element('#crumb').scope().addCrumbs(crumbs);
Can someone explain why this is not working? 有人可以解释为什么这行不通吗?
First of all, Angular is designed for single page applications and can simulate navigation between pages through modules like ngRoute (official) or ui-router (from Angular UI Team, more powerful, more complicated) . 首先,Angular是为单页应用程序设计的,并且可以通过ngRoute(官方)或ui-router(来自Angular UI Team,功能更强大,更复杂)之类的模块来模拟页面之间的导航。
Your example doesn't follow the "Angular way" cause you have two "index" page, I mean, Angular (and your app) is re-loaded each time you click on an internal link so you can't share variables between pages (in your case you want a "breadcrumb" shared between pages). 您的示例未遵循“ Angular方式”,因为您有两个“索引”页面,我的意思是,每次单击内部链接时都会重新加载Angular(和您的应用程序),因此您无法在页面之间共享变量(在您的情况下,您希望页面之间共享一个“面包屑”)。
Your breadcrumb
directive should share the breadcrumb
array via data binding instead of using an external controller explicitly. 您的
breadcrumb
指令应通过数据绑定而不是显式使用外部控制器来共享breadcrumb
数组。 See here for more infos . 有关更多信息,请参见此处 。
The ng-repeat
directive should be applied to the li
element instead of the ol
element. ng-repeat
指令应应用于li
元素而不是ol
元素。
See the updated version of your Plunkr . 请参阅Plunkr的更新版本 。 I added ngRoute module and separated pages.
我添加了ngRoute模块并分隔了页面。 BUT I think it's not a very reusable and clean way for breadcrumbs.
但是我认为这不是面包屑的非常可重用和干净的方式。
I recommend you to use a dedicated module to handle your breadcrumb, like ng-breadcrumb (see the demo here ) 我建议您使用专用模块来处理面包屑,例如ng-breadcrumb (请参见此处的演示 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.