如何CAN控制器的谈话在AngularJS指令? [英] How can controller talk to a directive in AngularJS?
问题描述
- 我有一个显示4闪存卡,其中3由一个指令填充一个应用程序
- 1的最后一张牌,我想价值取决于控制器上
-
在视图基于(以及相应的控制器),控制器可以在第4卡的价值填补。
- I have a app that displays 4 flash cards, 3 of them are populated by one directive
- for 1 last card, I would like the value to depend on a controller
Based on the view(and corresponding controller), controller can fill in the value of the 4th card.
为了演示,请参阅这里
问结果
- 我如何更新一些控制器4卡值结果?
- 即使它是从某些控制器指令将值一个好主意,结果?
- 什么可能是最好的解决方案?
Question
- How can I update the value in the 4th card from some controller?
- Is it even a good idea to push values from some controller to directive?
- What could be the best possible solution?
我是新手角并得到我的头周围
I am Angular newbie and getting my head around it
推荐答案
你的问题,我想你需要类似的东西:<一href=\"http://plnkr.co/edit/gp0zIwnj9Oz3IpQPXhDI?p=$p$pview\">http://plnkr.co/edit/gp0zIwnj9Oz3IpQPXhDI?p=$p$pview
我添加的数据中的指令的范围,该数据从控制器传递
for your question, I think you need something like that : http://plnkr.co/edit/gp0zIwnj9Oz3IpQPXhDI?p=preview
I added the data in the scope of your directive, this data is passed from the controller
scope: {
ngModel: '=',
somedata:'@'
},
中的HTML:
<data-ng-pt-header somedata='{{somedata}}'></data-ng-pt-header>
和控制器:
$scope.somedata='This comes from the controller';
和课程模板:
<div class="well info-card days-left">
<legend>Spent</legend>
<span>{{somedata}}</span>
</div>
这是众多方法将数据传递给一个指令,最简单的,如果你想在指令的详细信息也有这个优秀的职位之一:<一个href=\"http://amitgharat.word$p$pss.com/2013/06/08/the-hitchhikers-guide-to-the-directive/\">http://amitgharat.word$p$pss.com/2013/06/08/the-hitchhikers-guide-to-the-directive/
This is one of the many ways to pass data to a directive, the easiest, if you want more info on directives there is also this excellent post : http://amitgharat.wordpress.com/2013/06/08/the-hitchhikers-guide-to-the-directive/
有关的另外两个问题,是的,它可以将数据发送到来自控制器的指令是一个好主意,这取决于数据和您想要的逻辑很多,但你的应用程序似乎需要它。
最好的解决办法并不存在(至少不是看到只有一个简单的例子),但因为它似乎是一个简单的数据交换,去最简单的方法似乎满足您的要求;)
For the two other questions, yes, it could be a good idea to send data to a directive from a controller, it depends a lot on the data and the logic you want, but your app seems to need it.
The best solution doesn't exist (at least not with seeing only one simple example), but as it seems to be a simple data exchange, the easiest way to go seems to match your requirements ;)
有乐趣
这篇关于如何CAN控制器的谈话在AngularJS指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!