我想创建一个包含动态内容的表格。单击元素时,详细信息应显示在下一行中。所以我创建了以下内容:
<table ng-controller="TestCtrl">
<tr ng-repeat-start="word in ['A', 'B', 'C']">
<td><!-- Some Information, Image etc --></td>
<td ng-click="showDetails(word)">{{word}}</td>
</tr>
<!-- This is only visible if necessary -->
<tr ng-repeat-end ng-show="currentDetail == word">
<td colspan="2" ng-attr-id="{{'Details' + word}}"></td>
</tr>
</table>
我有以下js代码:
angular.module('maApp', []).controller("TestCtrl", function($scope, $document, $compile){
$scope.showDetails = function(word){
var target = $document.find("Details" + word);
//I checked it - target is NOT null here
//target.html("<div>Test</div>");
//target.append("<div>Test</div>");
var el = $compile("<div>Test</div>")($scope);
target.append(el);
//Show tr
$scope.currentDetail = word;
};
});
我也尝试了上面的评论解决方案,但没有任何效果(但是 tr 出现了)。我猜 $document.find("Details" + word)
有问题但我不知道是什么。
最终我想添加一个 <iframe>
源将包含 word
.
有人看到我在这里做错了什么吗?
最佳答案
不需要奇怪的非 Angular DOM 操作:您只需要这个。
HTML:
<table ng-controller="TestCtrl">
<tr ng-repeat-start="word in ['A', 'B', 'C']">
<td><!-- Some Information, Image etc --></td>
<td ng-click="showDetails(word)">{{word}}</td>
</tr>
<tr ng-show="currentDetail==word" ng-repeat-end>
<td colspan="2">Details {{word}}</td>
</tr>
</table>
JS:
angular.module('myApp', []).controller("TestCtrl", function($scope, $document, $compile){
$scope.showDetails = function(word) {
$scope.currentDetail = word;
};
});
关于javascript - 单击时Angularjs将元素添加到DOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33804793/