【问题标题】:Angularjs add element to DOM when clicking单击时Angularjs将元素添加到DOM
【发布时间】:2016-02-21 15:34:12
【问题描述】:

我想创建一个包含动态内容的表格。单击元素时,详细信息应显示在下一行中。所以我创建了以下内容:

<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) 有问题,但我不知道是什么问题。

最终我想添加一个&lt;iframe&gt;,而源将包含word

有人看到我在这里做错了吗?

【问题讨论】:

    标签: javascript html angularjs dom add


    【解决方案1】:

    你已经把所有你需要的东西都内置到了 Angular 中,你根本不需要 Javascript。

    this plunker example

      <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>
    

    【讨论】:

    • 感谢您的回答!可悲的是,我遇到了与@Fissio 的回答相同的问题:我想添加 iframe,并且有数千个 iframe(最后)网站变得无响应......
    • 这就是区别,我使用了ng-if 而不是ng-show,这会阻止你的内容在DOM中显示...所以如果项目只有iframe展开
    • 你说得对,我没看到。那么很好的答案:-)谢谢
    【解决方案2】:

    不需要奇怪的非棱角分明的 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;
        };
    });
    

    http://jsfiddle.net/HB7LU/20074/

    【讨论】:

    • 非常感谢您的回答。我知道我可以做到,但我有一个问题......(这就是为什么我写了我想添加iframes)。当我这样做时,我的网站上有数以千计的 iframe,它变得无响应......
    • 如果您担心性能,您可能应该使用ng-if 而不是ng-show。对于ng-if,在表达式计算为true 之前,DOM 中不存在相关元素,而ng-show 仅使用CSS 显示/隐藏。因此,ng-if 一次只能存在一个 iframe。该代码与前面的示例几乎相同。 jsfiddle.net/HB7LU/20075
    【解决方案3】:
    jqlite 中的

    $document.find 仅限于标签名称。您必须添加 jquery 才能获得更多信息。 查看docs 中支持的内容。

    【讨论】:

    • 感谢您的回答。我想在没有 jQuery 的情况下完成它。有 angularjs 的方法吗?
    • 顺便说一句:我也试过了:target = angular.element(document.getElementById(...)) 这也没有用
    • 您可以使用ngSanitizengBindngBindTemplatengBindHtml docs.angularjs.org/api/ng/directive/ngBindHtml
    • 听起来不错!我可以使用变量吗? {{word}} 在那个 html 里面?
    • 是的。您可以将 html 构建为刺痛,因此您基本上可以在控制器中执行此$scope.htmltobind = "&lt;div&gt;"+word+"&lt;/div&gt;";
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-28
    • 2012-05-31
    • 2016-01-27
    • 2016-05-17
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多