猿问

在ng-repeat内部的ng-click函数中添加参数似乎不起作用

我有一个ng-repeat像这样的简单循环:


<li ng-repeat='task in tasks'>

  <p> {{task.name}}

  <button ng-click="removeTask({{task.id}})">remove</button>

</li>

控制器中有一个功能$scope.removeTask(taskID)。


据我所知,Angular将首先渲染视图并用{{task.id}}数字替换插值,然后在单击事件时将评估ng-click字符串。


在这种情况下ng-click,完全可以得到预期的结果,即:ng-click="removeTask(5)".但是……它什么也没做。


当然,我可以编写代码以task.id从$tasks数组甚至DOM中获取代码,但这似乎不像Angular那样。


那么,如何ng-click在ng-repeat循环内向指令添加动态内容呢?


繁星coding
浏览 742回答 4
4回答

人到中年有点甜

对于在搜索中找到此内容的人来说,这也值得一提...<div ng-repeat="button in buttons" class="bb-button" ng-click="goTo(button.path)">&nbsp; <div class="bb-button-label">{{ button.label }}</div>&nbsp; <div class="bb-button-description">{{ button.description }}</div></div>注意的值ng-click。传递给的参数goTo()是绑定对象(button)的属性中的字符串,但没有用引号引起来。看起来AngularJS为我们处理了这个问题。我挂了几分钟。

长风秋雁

这可行。谢谢。我正在注入自定义html并在控制器中使用angular对其进行编译。&nbsp; &nbsp; &nbsp; &nbsp; var tableContent= '<div>Search: <input ng-model="searchText"></div>'&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; +'<div class="table-heading">'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; +&nbsp; &nbsp; '<div class="table-col">Customer ID</div>'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ ' <div class="table-col" ng-click="vm.openDialog(c.CustomerId)">{{c.CustomerId}}</div>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $timeout(function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var linkingFunction = $compile(tableContent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var elem = linkingFunction($scope);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // You can then use the DOM element like normal.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jQuery(tablePanel).append(elem);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("timeout");&nbsp; &nbsp; &nbsp; &nbsp; },100);
随时随地看视频慕课网APP

相关分类

AngularJS
我要回答