将方法从控制器注入到未使用右变量调用的指令
Inject method from controller to a directive not calling with right variable
我正在学习AngularJS,在将方法作为属性"注入"自定义指令时遇到问题
<div ng-controller="LoLImagesController as lolImgCtrl">
<ul class="list-inline thumbs">
<li id="champThumb" class="thumbnail" ng-repeat="championName in lolImgCtrl.keysToShow()">
<champion-thumbnail champion="lolImgCtrl.imageData.champions[championName]"
champion-image="lolImgCtrl.imageData.images[championName]"
champion-info="lolImgCtrl.infoData.champions[championName].info"
method="lolImgCtrl.setChampForModal(championName)">
</champion-thumbnail>
</li>
</ul>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">{{lolImgCtrl.champForModal}}</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
正如您所看到的,在第7行中,我试图在champion-thumbnail
指令的method
属性中放置一个方法。
冠军缩略图.html
<!DOCTYPE html>
<div>
<div id="champName">
<button type="button" class="btn btn-info btn-lg" data-toggle="modal"
data-target="#myModal"
ng-click="method({championName: '{{champion.key}}'})">
info
</button></br>
<b>{{champion.name}}</b></br>
{{champion.title | capitalize}}</br>
</div>
<div class="progress">
<div class="progress-bar progress-bar-warning" role="progressbar" style="width: {{championInfo.defense}}0%">
<span>Defense : {{championInfo.defense}} / 10</span>
</div>
</div>
<div class="progress">
<div class="progress-bar progress-bar-info" role="progressbar" style="width: {{championInfo.magic}}0%">
<span>Magic : {{championInfo.magic}} / 10</span>
</div>
</div>
<div class="progress">
<div class="progress-bar progress-bar-success" role="progressbar" style="width: {{championInfo.difficulty}}0%">
<span>Difficulty : {{championInfo.difficulty}} / 10</span>
</div>
</div>
<div class="progress">
<div class="progress-bar progress-bar-danger" role="progressbar" style="width: {{championInfo.attack}}0%">
<span>Attack : {{championInfo.attack}} / 10</span>
</div>
</div>
<img ng-src="{{championImage}}">
</div>
您可以看到,我试图调用在ng-click
属性中的指令内部移动的方法。
冠军缩略图指令定义
app.directive('championThumbnail', function () {
return {
restrict: 'E',
scope: { champion: '=', championImage: '=', championInfo: '=', method: '&' },
templateUrl: 'champion-thumbnail.html'
};
});
方法转移到指令:setChampForModal
this.setChampForModal = function (champ) {
$log.debug("set " + champ + " for modal");
this.champForModal = champ;
};
实际问题
当我检查按钮时,html显示:
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal" ng-click="method({championName: 'Aatrox'})">info</button>
但当我点击按钮时,模态打开,显示标题{{champion.key}}
而不是实际值(在示例中为Aatrox
)
我在这里错过了什么?当然,我的最终目标是获得实际值,并用一个控制器包装模态,该控制器将显示我想要的数据,但目前,我希望它在同一个控制器下,但它仍然显示不正确。
p.S:函数有一个$log.debug
调用,显示set {{champion.key}} for modal
。
编辑:我是AngularJS和webdev的绝对初学者。欢迎任何形式的提示和/或建设性的批评!
我认为问题出在这个上
`<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal" ng-click="method({championName: '{{champion.key}}'})">`
ng-click不会将{{champion.key}}
解析为其值,而是将其作为字符串。
尝试
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-arget="#myModal" ng-click="method({championName: champion.key})">
而
相关文章:
- 从带参数的字符串变量调用函数中的函数
- 将方法从控制器注入到未使用右变量调用的指令
- 为什么可以对整数变量调用toString(),而不能对文字数字调用
- 我可以对变量调用“.log”吗?
- 是否可以对超出范围的 setInterval 变量调用 clearInterval 方法
- 对模板中的胡子变量调用方法
- 使用不同的变量调用递归函数
- Jquery中的变量调用
- 将循环变量调用到自动压缩选择中
- 如何在 jquery 中用变量调用变量
- jQuery,从变量调用数组对象的一部分
- 在 AngularJS 中使用绑定变量调用控制器方法
- 如何使用变量调用对象及其方法
- 是否可以对变量调用jquery函数
- Android:从javascript传递变量调用Java中的函数
- 带有变量调用的谷歌地图API实现
- 无法对数字变量调用 toFixed
- 如何将嵌套的全局或本地jquery变量调用到html中
- Javascript:如何通过变量调用函数并传递参数
- 对字符串变量调用toUppercase()