[英]Angularjs add element to DOM when clicking
I want to create a table which contains dynamic Content. 我想创建一个包含动态内容的表。 When clicking on an element, the Details should Show up in the next line. 单击元素时,详细信息应显示在下一行中。 So i creatied the following: 所以我创造了以下内容:
<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>
And I have the following js code: 我有以下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;
};
});
I also tried the commented Solutions above but nothing of it works (The tr is showing up however). 我也尝试了以上评论的解决方案,但没有任何工作(但是tr正在出现)。 I guess there is something wrong with $document.find("Details" + word)
but I don't know what. 我猜$document.find("Details" + word)
但我不知道是什么。
Ultimately I want to add an <iframe>
and the source would contain the word
. 最后我想添加一个<iframe>
,源代码将包含该word
。
Does anybody see what I'm doing wrong here? 有人看到我在这里做错了吗?
No need for weird non-angulary DOM manipulation: All you need is this. 不需要奇怪的非角度DOM操作:所有你需要的就是这个。
HTML: 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: JS:
angular.module('myApp', []).controller("TestCtrl", function($scope, $document, $compile){
$scope.showDetails = function(word) {
$scope.currentDetail = word;
};
});
http://jsfiddle.net/HB7LU/20074/ http://jsfiddle.net/HB7LU/20074/
you have all you need built into angular already, you don't need the Javascript at all. 你已经将所有需要内置到角度中,你根本就不需要Javascript。
see this plunker example 看到这个plunker的例子
<table style="width:100%;">
<thead style="background-color: lightgray;">
<tr>
<td style="width: 30px;"></td>
<td>
Name
</td>
<td>Gender</td>
</tr>
</thead>
<tbody>
<tr ng-repeat-start="person in people">
<td>
<button ng-if="person.expanded" ng-click="person.expanded = false">-</button>
<button ng-if="!person.expanded" ng-click="person.expanded = true">+</button>
</td>
<td>{{person.name}}</td>
<td>{{person.gender}}</td>
</tr>
<tr ng-if="person.expanded" ng-repeat-end="">
<td colspan="3">{{person.details}}</td>
</tr>
</tbody>
</table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.